Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to run a detached screen under a specific user in rc.local on boot. The code below is what I have so far, but it is not working for me. The su part seems to be giving me an error.

su - username -c /usr/bin/screen -dmS test bash -c '/var/www/path/to/script/script.sh; exec bash'
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

I think both -c parameters (su and bash) will have to be quoted at a minimum.

su - username -c "/usr/bin/screen -dmS test bash -c '/var/www/path/to/script/script.sh; exec bash'"

Also - is expected to be last and may not be desirable here (see man su).

A few more remarks. sudo could be a better choice for a one-shot command like yours, but not necessarily (also see the comment below my answer, it's what I had in mind when I wrote this). Another thing you want to be careful with is executing commands without absolute path in a privileged context. This holds for su (or sudo) but also for the path to bash in your command. You are doing it right with screen.

Just tested the following and it works nicely. I think the - is the main issue in your original line:

/bin/su username -c "/usr/bin/screen -dmS test bash -c '/home/username/test.sh; exec bash'"

Evil remark: why don't you give tmux a try? I have recently switched and never looked back. The only thing I needed to change immediately was the prefix key combination which in tmux defaults to Ctrl + B - back to GNU screen's Ctrl + A.

It allows splitting your window into an almost arbitrary number of panes (vertically and horizontally) and its configuration file format (including the one for the status par) is actually intelligible to humans. Of course tmux is as good as screen when you simply want to run some program/script not originally written as daemon in background. If you intend to interact with the terminal multiplexer, however, I warmly recommend tmux.

share|improve this answer
I will give this a try in a few. The script I am running uses absolute paths, so would you recommend su or sudo? –  ATLChris Feb 27 '13 at 13:03
sudo is not a good choice if run from rc.local where you already are root, since you will miss a login shell. On a side note, have a look at /etc/init/tty1.conf - replace [a]getty with screen here. –  aquaherd Feb 27 '13 at 13:12
@aquaherd: it's what I had in mind when I wrote but not necessarily. The clue, really, is in the usual sudo su - ... sudo (superuser do), su (switch user) ... good idea about the .conf –  0xC0000022L Feb 27 '13 at 14:10
add comment

Try sudo -u username instead of su - username

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.