I want to run a python script at boot as a particular user.

The script is a server (listens on a socket, serves requests, etc) and should be run in the background, with any output preferably piped to a log file.

My current approach is to add an entry at the end of rc.local like this:

su -l usr -c 'script.py arg1 arg2 &> logfile.log &'

The problem: In the script i have several print statements in the script. They never show up in the log file. I've tried

su -l usr -c 'script.py arg1 arg2 &> logfile.log &'

su -l usr -c 'script.py arg arg &> logfile.log &' &> secondLogfile.log &

su -l usr -c 'script.py arg arg &> logfile.log &'

su usr -c 'script.py arg arg &> logfile.log &'

I have the feeling i'm missing something basic. Maybe i'm going about it in totally the wrong way.

System is Fedora 15, script is Python3 and runs fine from the command line as the user. User has write permissions to the folder i want the output redirected to. (I ruled it out by making the folder 777).

I tried having the python script run as a Systemd service, but i was having issues with my script being able to grab a socket to listen on. (throwing an OS error about socket in use if i enabled the systemd service at boot, whereas if i ran systemctl start script.service after boot things worked fine).

How can i launch a python script as a user at boot that grabs a socket?

  • 2
    can you not just use a @reboot line in a /etc/cron.d/ file ? – Sirex Aug 23 '12 at 2:01

This works for me:

su -l usr -c "python script.py arg1 arg2 > logfile.txt &"

I believe the previous & is pushing "python script.py" into the background prematurely.


The @Reboot line worked like a charm in crontab. Freetx's answer didn't work for me ( i tried something exactly like it and no dice :-(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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