Saturday, January 29, 2011

Can I send some text to the STDIN of an active process running in a screen session?

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?

  • 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
    
    Cristian Ciupitu : 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.
    From krissi
  • 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
    
    James Lawrie : +1 I did not know that. I need to look more into the magic of proc
    Cristian Ciupitu : @James Lawrie: then have a look at [proc(5)](http://manpages.courier-mta.org/htmlman5/proc.5.html) and [proc.txt](http://kernel.org/doc/Documentation/filesystems/proc.txt).
    troyengel : +2 no matter how much you think you know, there's always more to learn :) slick.

0 comments:

Post a Comment