4

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
PID TTY          TIME CMD
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?

2
  • 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
    Commented Apr 9, 2019 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. Commented Apr 9, 2019 at 12:45

2 Answers 2

11

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
  PID TTY      STAT   TIME COMMAND
 # ...
 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
 # ...
0
0

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 &

(ctrl-d)
3
  • (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) Commented Apr 10, 2019 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. Commented Apr 10, 2019 at 20:50
  • I’ve made it more clear. Was just pasting a real life example and hadn’t considered the confusion. Sorry.
    – Strainger
    Commented Apr 10, 2019 at 20:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .