To run a command when the system boots, schedule it for @reboot
in cron. See man 5 crontab
for details. This means your crontab line should look like
@reboot screen -m ...
Don't use su
for that, put the command in the crontab of the user who should run the command. That way, the user can manage the commands without root's intervention. (But if you wanted to run a command as a system user without letting that system user change the command for security reasons, somethnig like su -c /path/to/command www-data
in /etc/rc.local
would be better.)
Screen closes a window when its command finishes and terminates the session when its last window is closed. If you want to see the script's output after it's finished, run another command after it to wait for input. For example, to leave the window open until you press Enter in it:
screen sh -c './script.sh; read'
Remember that screen only keeps a finite number of history lines. Unless script.sh
sometimes requires interaction, you would be better served by redirecting its output to a file. If it does require interaction, you can use screen's log
command to send output to a file (and then it's not a problem if the screen session terminates when it doesn't require input).
That's useful thanks. Any reason why I should use cron over other methods? – Jacob Hawkins – 2010-10-11T21:17:56.203
@Jacob: see my expanded second paragraph. – Gilles 'SO- stop being evil' – 2010-10-11T21:41:36.330