I'm running the sleep command in terminal using screen and in detached mode.
Once the screen immediately returns, I'm running ps command to verify the sleep is running.

$ screen -d -m 'sleep 2m'
[raj@localhost ~]$ ps
22795 pts/0    00:00:00 bash
22869 pts/0    00:00:00 ps

But the command didn't show sleep. What is that I'm doing wrong here?

  • why would you want to run sleep through a detached screen ? couldn’t you just run it as sleep 2m & directly from your shell ? besides, ps alone shows only processes belonging to your terminal window, while whatever you run through screen will belong to a different virtual terminal – LL3 Apr 9 at 12:08
  • @LL3 - I'm doing it as a poc to run a command from remote machine using screen and detach from screen. I have tried using ps -a | grep sleep still it is not showing any new process running sleep. – Rajkumar Natarajan Apr 9 at 12:45

This was confusing to me initially as well. I then re-read the local screen man page for the SYNOPSIS -- the online man page does not give a synopsis) -- and noticed that it said:

screen [ -options ] [ cmd [ args ] ]

... which led me to believe that it wanted to see the cmd and args as independent arguments.

Since you gave that first argument as a quoted value -- 'sleep 2m' -- it tried to execute a command named (exactly) 'sleep 2m', as opposed to what you really wanted, which was sleep with its own argument of 2m. The screen command exited successfully (in my testing), but it did not successfully execute your command.

Use, instead:

screen -d -m sleep 2m

Instead of ps, which will only show processes associated with the current terminal (of which the SCREEN and related processes are not), use:

ps x

which will show it:

$ ps x
 # ...
 7514 pts/1    Ss     0:00 -bash
 7761 ?        Ss     0:00 SCREEN -d -m sleep 2m
 7762 pts/2    Ss+    0:00 sleep 2m
 7880 pts/1    R+     0:00 ps x
 # ...
  • You say “screen expected the command in the first argument”, and then you show a command line where the secondary command (the command to be invoked by the primary command) is spread out over argv[3] and argv[4]. I expect that some people will be confused by this. – G-Man Apr 10 at 15:27
  • Would it help if I said "first non-option argument"? I intended the translation to be from "'sleep 2m'" to "sleep", but I want to reduce confusion, not generate more! – Jeff Schaller Apr 10 at 17:40
  • 1
    I suppose that would be less unclear, but not really better.  Maybe you want to say that the secondary command verb and the arguments to the secondary command must be separate arguments to the primary command, like with find -exec, sudo and xargs, and different from the way sh -c and some other commands require it. – G-Man Apr 10 at 17:50
  • I've updated the post to include some more analysis of the commands & args. Please take a look and see if that helps, and let me know if you see any further improvements. Thank you! – Jeff Schaller Apr 10 at 18:28
  • Very good.  I might have mentioned find -exec, sudo, xargs, and sh -c in the answer, but perhaps that would be information overload.  If we leave my second comment, I don’t believe that the answer needs any more changes. – G-Man Apr 10 at 20:27

Would using nohup suffice? It will show in ps where as running it in screen may make the process hidden within the screen process.

An example of this would be from bash..

nohup your-command &

If you want to exit this terminal session you may want to use an at job command..

at now + 1 minute

nohup your-command &

  • (1) This is confusing.  You talk about a script and redirecting the standard output, neither of which is in the question.  On the other hand, the question features a sleep command, which your answer doesn’t mention. (2) Can you explain why you believe that nohup will help?  Do you believe that some process (e.g., sleep) is getting a hangup signal? … (Cont’d) – G-Man Apr 10 at 20:50
  • (Cont’d) … (3) Using nohup would probably not suffice.Jeff’s answer presents two reasons why the user is not seeing the result they’re expecting. Adding nohup might be necessary also, but, by itself, it’s not likely to be enough. – G-Man Apr 10 at 20:50
  • I’ve made it more clear. Was just pasting a real life example and hadn’t considered the confusion. Sorry. – Strainger Apr 10 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.