User Tools

Site Tools


run_program_at_boot_using_cron

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
run_program_at_boot_using_cron [2017/01/31 16:29] – [Testing the Launcher.sh] walkeradminrun_program_at_boot_using_cron [2023/03/09 22:35] (current) – external edit 127.0.0.1
Line 3: Line 3:
 \\  \\ 
 \\  \\ 
-==== Introduction ====+{{:pocket_watch.jpg?200|}}
 \\  \\ 
 +
 +----
 +
 +===== 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>launcher.sh</color>. 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>launcher.sh</color>.
-\\  
 \\  \\ 
  
Line 60: Line 63:
 sudo chmod 755 launcher.sh sudo chmod 755 launcher.sh
 </file> </file>
-\\ 
  
 ---- ----
 +==== 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:
 +\\ 
 +<file>
 +sudo mkdir logs
 +</file>
 +You should now have a logs folder, use ls to check.
 +\\ 
 +\\ 
 +
 +----
 +
  
 ==== Testing the Launcher.sh ==== ==== Testing the Launcher.sh ====
 \\  \\ 
 Before adding as a Cron job, it is a good idea to test the script, to do this enter: Before adding as a Cron job, it is a good idea to test the script, to do this enter:
-\\  
 \\  \\ 
 <file> <file>
Line 74: Line 88:
 \\  \\ 
 Your script/program should now run. If it does then you can move on to the Cron part. Your script/program should now run. If it does then you can move on to the Cron part.
 +\\ 
 +
 +----
 +==== 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)</color>
 +\\ 
 +\\ 
 +<file>
 +sudo crontab -e
 +</file>
 +\\ 
 +Scroll to the bottom of the Cron file and enter:
 +\\ 
 +<file>
 +# Cron Job to start script/program at boot
 +@reboot sh /home/pi/launcher.sh >/home/pi/logs/cronlog 2>&1
 +</file>
 +\\ 
 +When you boot/reboot your Raspberry Pi, this will now run the launcher.sh, and if necessary, output any errors in the logs folder.
 \\  \\ 
 \\  \\ 
  
 ---- ----
 +
 +==== 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://linux.about.com/od/linux101/fl/sh-Linux-Command-Unix-Command.htm
 +
 +----
 +
 +\\ 
 +\\ 
 +
  
run_program_at_boot_using_cron.1485880178.txt.gz · Last modified: 2023/03/09 22:35 (external edit)