Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ./ 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?


share|improve this question

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 './; read'

Remember that screen only keeps a finite number of history lines. Unless 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).

share|improve this answer
That's useful thanks. Any reason why I should use cron over other methods? – Jacob Hawkins Oct 11 '10 at 21:17
@Jacob: see my expanded second paragraph. – Gilles Oct 11 '10 at 21:41

Have you tried screen -AmdS test bash -i ./ ?

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.

share|improve this answer
As an honored user of Super User, please consider revising you answer to provide more detail. – KronoS Jul 13 '12 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

share|improve this answer
Thanks, why should I use rc.local instead of cron or the other startup methods? – Jacob Hawkins Oct 11 '10 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 '10 at 3:16

You must log in to answer this question.

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