I have a long-running server process inside a screen session on my Linux server. It's a bit unstable (and sadly not my software so I can't fix that!), so I want to script a nightly restart of the process to help stability. The only way to make it do a graceful shutdown is to go to the screen process, switch to the window it's running in, and enter the string "stop" on its control console.

Are there any smart redirection contortions I can do to make a cronjob send that stop command at a fixed time every day?

link|improve this question
feedback

2 Answers

Write to /proc/*pid of the program*/fd/0.

Example

Terminal 1:

[ciupicri@hermes ~]$ cat
xxx

Terminal 2:

[ciupicri@hermes ~]$ pidof cat
7417
[ciupicri@hermes ~]$ echo xxx > /proc/7417/fd/0
link|improve this answer
+1 I did not know that. I need to look more into the magic of proc – James Lawrie Sep 6 '10 at 12:25
1  
@James Lawrie: then have a look at proc(5) and proc.txt. – Cristian Ciupitu Sep 6 '10 at 12:46
+2 no matter how much you think you know, there's always more to learn :) slick. – troyengel Sep 6 '10 at 14:55
feedback

Try this to start:

# screen
# cd /path/to/wd
# mkfifo cmd
# my_cmd <cmd
C-A d

And this to kill:

# cd /path/to/wd
# echo "stop" > cmd
# rm cmd
link|improve this answer
This is good, but it might have the disadvantage of not being able to send other commands while the program is running. If the program stops when it hits EOF on stdin then on the first echo "xxx" > cmd the program will stop (because the pipe will be closed). Though some programs are smart enough to reopen (rewind(3)) their stdin when they encounter EOF. – Cristian Ciupitu Sep 6 '10 at 12:30
feedback

Your Answer

 
or
required, but never shown