0

I need to have a shell script run at start up, but can't get it working. There seems to be a number of options, but what is the best method for having a script auto run when the system boots up, before a user is logged in? The script then runs a python program that runs constantly in the background. Thanks

Emanef
  • 1
  • The best method a matter of opinion - it's what you understand and can maintain. You can use cron, rc.d, systemd services, hook scripts, etc. What does your script need from the system? Filesystem? Network? Hardware? Display? – user535733 Jun 05 '17 at 11:25
  • Systemd is great for this and simple to setup – George Udosen Jun 05 '17 at 11:42
  • The .sh script is a simple script to run a Python program. The Python program is something that runs in the background monitoring what is played on my Sonos speakers and logging them. It needs permission to be able to edit, write and delete the log files it creates, etc and when I run it manually it has to be run with sudo – Emanef Jun 05 '17 at 11:55

1 Answers1

0

This is quite opinionated, but I use to run a game server for my grand-kids and used Crontab to backup the server and initiate the server start up automatically. Worked very well for me. Use this to complete your setup.

  • What should I add to the bottom of crontab for it to run? I've tried both of these (individually!) but neither work: @reboot /home/emanef/.local/bin/myscript.sh AT reboot su emanef /home/emanef/.local/bin/myscript.sh (that second one was also @ but I couldn't post AT reboot twice!) – Emanef Jun 05 '17 at 13:10
  • I'm in Windows right now but {man crontab} should have all the commands. Try this tutorial http://www.raspberrypi-spy.co.uk/2013/07/running-a-python-script-at-boot-using-cron/ – EODCraft Staff Jun 05 '17 at 14:17
  • No, can't get it to work. I've added an & to the end, tried both ways, and also tried doing it directly to the python script (adding python before /home) :( – Emanef Jun 06 '17 at 08:57
  • Perhaps paste your Cron Job to the question and one of the experts may be able to help. (You are using {sudo crontab} right)? – EODCraft Staff Jun 06 '17 at 09:25
  • Yes, definately running with sudo, just doesn't seem to like it – Emanef Jun 20 '17 at 09:12