How do I create several screen sessions on startup in Linux?
Solution 1
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).
Solution 2
Have you tried screen -AmdS test bash -i ./script.sh
?
I imagine that what is happening is it starts screen, which runs the script, and then when the script exits, screen terminates. the bash -i
part tells screen to launch bash in interactive mode, and then have bash run the script. Once the script it done executing, bash will continue to wait at a command prompt, holding the screen session open for you to attach to once you log into the system.
Solution 3
Create a script that does what you want it to. If switching between users for only a single command, then go ahead and use su -c
. Otherwise, create a separate script and call that one.
Then, to get your command to run at startup, put the command to run your script in /etc/rc.d/rc.local
Related videos on Youtube
Jacob Hawkins
Updated on September 17, 2022Comments
-
Jacob Hawkins over 1 year
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!
-
Jacob Hawkins over 13 yearsThanks, why should I use rc.local instead of cron or the other startup methods?
-
Jacob Hawkins over 13 yearsThat's useful thanks. Any reason why I should use cron over other methods?
-
Gilles 'SO- stop being evil' over 13 years@Jacob: see my expanded second paragraph.
-
Kevin M over 13 yearsFor the most part they're equivalent. The part where they differ is precisely when they run the command. /etc/rc.d/rc.local is the last item to run, after all the other services have started. You'll want to check your manpage for when @reboot runs.
-
Prerak Diwan almost 12 yearsAs an honored user of Super User, please consider revising you answer to provide more detail.