Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send stuff commands to a start-in-detached screen?

Any session that I start with "-d -m" doesn't accept "-X stuff [...]" commands unless I've attached to the screen at least once. There is no error message, the commands just do not get through.

The problem is that I start the session from a cron job and am unable to attach to the screen from within cron.

Steps to repeat

$ screen -m -d -S mydaemon bash
$ screen -S mydaemon -X stuff "`printf "exit\\r"`"
$ screen -ls
        32456.mydaemon  (Detached)
$ screen -r -S mydaemon
$ ^a d
$ screen -S mydaemon -X stuff "`printf "exit\\r"`"
$ screen -ls
No Sockets found in /var/run/screen/S-user

^a d indicates pressing Ctrl+a then pressing d.

Versions

CentOS release 5.5 (Final)
Screen version 4.00.03 (FAU) 23-Oct-06
like image 777
Josh Brown Avatar asked Dec 14 '10 15:12

Josh Brown


1 Answers

Edit: The best answer to this question is this other SO answer. I leave my kludgey solution here, anyway, in case it inspires a solution to a similar problem.


A possible workaround is to use a second, already running and detached screen session to start the screen session to which you want to send the "stuff" command in attached mode, and then send that screen session a detach command and then the stuff command.

$ screen -dmS spawner
$ screen -S spawner -X screen screen -dR mydaemon
$ sleep 1 # may be necessary
$ screen -S mydaemon -X detach
$ screen -S mydaemon -X stuff "whatever"

(Note: the doubled "screen" is not a typo!) You are still left with an unstuffable screen session (spawner) but if the resources it takes are important you can always just use "kill -TERM ..." or its ilk to terminate it (or have it automatically exit after a certain amount of time by starting it with something like

$ screen -dmS spawner bash -c "sleep 60"

or similar).

like image 70
Ron Kaminsky Avatar answered Oct 23 '22 10:10

Ron Kaminsky