run_program_at_boot_using_cron
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
run_program_at_boot_using_cron [2017/01/31 16:22] – walkeradmin | run_program_at_boot_using_cron [2017/01/31 22:14] – [Introduction] walkeradmin | ||
---|---|---|---|
Line 3: | Line 3: | ||
\\ | \\ | ||
\\ | \\ | ||
- | ==== Introduction ==== | + | {{: |
\\ | \\ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Introduction ===== | ||
I was using the .bashrc file when logged in as a user (from your home directory, type sudo nano .bashrc) to launch scripts upon the Raspberry Pi bootup. However, while this works, its has one really annoying issue. If you login from another place, say from SSH, then the script runs again, which isn't great for my application. | I was using the .bashrc file when logged in as a user (from your home directory, type sudo nano .bashrc) to launch scripts upon the Raspberry Pi bootup. However, while this works, its has one really annoying issue. If you login from another place, say from SSH, then the script runs again, which isn't great for my application. | ||
\\ | \\ | ||
Line 15: | Line 19: | ||
\\ | \\ | ||
To avoid putting loads of commands in to Cron, its best to group them all in to a file, then just reference that file in Cron. For this example we will make a file called <color red> | To avoid putting loads of commands in to Cron, its best to group them all in to a file, then just reference that file in Cron. For this example we will make a file called <color red> | ||
- | \\ | ||
\\ | \\ | ||
Line 45: | Line 48: | ||
sudo python nameOfScriptorProgram & | sudo python nameOfScriptorProgram & | ||
</ | </ | ||
+ | Be sure to replace the part <color red> | ||
\\ | \\ | ||
+ | \\ | ||
+ | Save the file by using Ctrl-X and answering Yes. | ||
+ | \\ | ||
+ | \\ | ||
+ | |||
+ | ---- | ||
+ | ==== Execution Rights ==== | ||
+ | \\ | ||
+ | The script will not run unless it is made executable. to do this enter the following: | ||
+ | \\ | ||
+ | < | ||
+ | sudo chmod 755 launcher.sh | ||
+ | </ | ||
+ | \\ | ||
+ | |||
+ | ---- | ||
+ | ==== Add a logs folder ==== | ||
+ | \\ | ||
+ | In case we get any cron errors, its best to add a folder that we can output any cron logs to, in the same folder that your script or program is, enter the following: | ||
+ | \\ | ||
+ | < | ||
+ | sudo mkdir logs | ||
+ | </ | ||
+ | You should now have a logs folder, use ls to check. | ||
+ | \\ | ||
+ | \\ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | |||
+ | ==== Testing the Launcher.sh ==== | ||
+ | \\ | ||
+ | Before adding as a Cron job, it is a good idea to test the script, to do this enter: | ||
+ | \\ | ||
+ | < | ||
+ | ./ | ||
+ | </ | ||
+ | \\ | ||
+ | Your script/ | ||
+ | \\ | ||
+ | |||
+ | ---- | ||
+ | ==== Add Launcher.sh to Cron ==== | ||
+ | \\ | ||
+ | Finally we need to add the launcher.sh to Cron. to do this we need to enter the following <color red>(if Cron has never been run, you will get a warning, just continue and a new Cron file will be created)</ | ||
+ | \\ | ||
+ | \\ | ||
+ | < | ||
+ | sudo crontab -e | ||
+ | </ | ||
+ | \\ | ||
+ | Scroll to the bottom of the Cron file and enter: | ||
+ | \\ | ||
+ | < | ||
+ | # Cron Job to start script/ | ||
+ | @reboot sh / | ||
+ | </ | ||
+ | \\ | ||
+ | When you boot/reboot your Raspberry Pi, this will now run the launcher.sh, | ||
+ | \\ | ||
+ | \\ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ==== References ==== | ||
+ | \\ | ||
+ | This originally came from the following website, I have copied it here in case this site ever goes down. I'd like to say thank you to the original author. | ||
+ | \\ | ||
+ | \\ | ||
+ | http:// | ||
+ | |||
+ | ---- | ||
+ | |||
+ | \\ | ||
+ | \\ | ||
+ | |||
+ |
run_program_at_boot_using_cron.txt · Last modified: 2023/03/09 22:35 by 127.0.0.1