Firstly, what's the best way to execute commands on startup, cron? Can I use su
in a shell script to switch between different users, if so how?
How do I create several detached screen
sessions on startup? screen -A -m -d -S test ./script.sh
seems like it should work but using it in a script started by cron doesn't show any screen sessions running after booting.
It looks like the screen session is closed after the command finishes executing, can I keep it open so I can see the output?
Thanks!
Best Answer
To run a command when the system boots, schedule it for
@reboot
in cron. Seeman 5 crontab
for details. This means your crontab line should look likeDon'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 likesu -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:
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'slog
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).