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'
Best Answer
I think both
-c
parameters (su
andbash
) will have to be quoted at a minimum.Also
-
is expected to be last and may not be desirable here (seeman su
).A few more remarks.
sudo
could be a better choice for a one-shot command like yours, but not necessarily:In particular, you can use one less level of quoting with
sudo
.Another thing you want to be careful with is executing commands without absolute path in a privileged context. This holds for
su
(orsudo
) but also for the path tobash
in your command. You are doing it right withscreen
.Just tested the following and it works nicely. I think the
-
is the main issue in your original line: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 intmux
defaults to Ctrl + B - back to GNUscreen
'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 asscreen
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 recommendtmux
.