~adeodato/ blog/ entries/ 2008/ 02/ 16/ Non-interactively starting a screen with several windows

Non-interactively starting a screen with several windows

With /usr/bin/screen -dm you can start a screen non-interactively, eg. from cron, passing the command to run and its arguments as normal arguments:

  @reboot    screen -dm rtorrent -p 12345-12346

However, I wanted to start from cron a screen that would run various commands, each in its own window. AFAIK you can’t do this with a simple invocation, but you can prepare a custom screenrc file that defines which commands to run, with the screen command. For example:

  % cat ~/.screenrc_foo
  escape "^Oo"
  startup_message off
  hardstatus alwayslastline "%w"

  screen 0 command0 -x -y
  screen 1 command1 -z -w
  screen -t windowname 2 command2
  screen -t windowname 3 sh -c 'cd ~/quux && ./wrapper.sh'

Then you start it from cron like this:

  @reboot    screen -dm -c ~/.screenrc_foo

Thanks to Luca Capello and Romain Francoise in #debian-devel for the tip.

Update: Laurence J. Lane points on IRC the -X option to screen, so you could do without the screenrc file and do it like this:

  % screen -dmS NAME command0 -x -y
  % screen -S NAME -X screen command1 -z -w
  % screen -S NAME -X screen -t windowname command2
  ...