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?


3 Answers 3


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? Oct 11, 2010 at 21:17
  • @Jacob: see my expanded second paragraph. Oct 11, 2010 at 21:41

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.

  • As an honored user of Super User, please consider revising you answer to provide more detail. Jul 13, 2012 at 4:23

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

  • Thanks, why should I use rc.local instead of cron or the other startup methods? Oct 11, 2010 at 21:13
  • For 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.
    – Kevin M
    Oct 12, 2010 at 3:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .