3

I am trying to run a command in screen mode using the command

screen -dmS screen_name sed -i 's/a/b/'g some-file.txt

Nothing happens. When I put the same command in a script and run the command:

screen -dmS screen_name bash -c /path/to/script

It works. My question is, can I run a command in daemon mode without first having to put it in a script? Basically, I need this daemon feature because it helps in running several commands in parallel, by running several sed commands on large files in parallel by throwing each command on a separate screen daemon, which terminates automatically after the program finishes. Thanks

muru
  • 197,895
  • 55
  • 485
  • 740
Mijo
  • 293

2 Answers2

4

I guess the issue is with the -S if you try to omit the -S option, it should ,work, even without bash -c so try this

screen -dm sed -i 's/a/b/'g some-file.txt

That should work. BTW screen is not updated, you should consider switching to tmux. It can provide you with much more features.

You can install tmux by typing:

sudo apt-get install tmux

So your code should look like:

tmux new-session -d -s foo 'sed -i 's/a/b/'g some-file.txt'

I could test it with

tmux new-session -d -s hello 'top'

if you type

tmux attach -t hello

It will take you to a session with top. Hope that this helps. check

man tmux

for all features and check here for a comprehensive cheat sheet

Mijo
  • 293
2

Does this work for you?

screen -dmS screen_name bash -c "sed -i 's/a/b/'g some-file.txt"
Oliver R.
  • 377
  • I tried with single quotes, and double quotes, no luck :( – Mijo Apr 16 '15 at 13:27
  • Can you please explain why you want to run such a command inside of screen? – Oliver R. Apr 16 '15 at 13:35
  • I am editing huge files with sed in order to replace some special characters. Sed usually runs using one processor, which could take hours. Running in daemon mode enables several commands to run in parallel. – Mijo Apr 16 '15 at 13:51
  • If you just want to achieve backgrounding: Use a single ampersand at the end of your command. Run the sed command within a screen session with an ampersand to not bind the processes on your session. Then use the command jobs to view running tasks. – Oliver R. Apr 16 '15 at 14:08
  • I see where you are going with this, but that wouldn't work inside a script. – Mijo Apr 16 '15 at 16:12
  • It will if you nohup it. But then it won't be accessible via jobs - it will truly daemonize. – JonB Apr 16 '15 at 17:00
  • I just tested my solution and for me it seems to work:
    cat some-file.txt f ff fff ; screen -dmS screen_name bash -c "sed -i 's/f/g/'g some-file.txt" ; cat some-file.txt g gg ggg
    – Oliver R. Apr 17 '15 at 06:00