0

I am in day 5 of my crash course of learning how to use a Raspberry Pi (or any Linux platform) for the first time. I have an .sh named "startup.sh" file located at "/home/pi/". I have tried several different forums and nothing has helped. I just want this .sh to run at startup.

The script I have made, mounts 2 drives from a wireless home server, and once connect it starts a slide show from some pictures on drives using a screensaver method. I needed there to be a delay between connecting to the wifi before mounting the drives, and a few seconds before it starts the slide show (to allow the mounting process to happen). I've tried using the ./superscript in the .bashrc (accessed using sudo nano .bashrc). I've also tried using the crontab by adding "@reboot /home/pi/startup.sh" at the end. Here is what my crontab and script look like. Link

user46629
  • 11
  • 1
  • 6
  • 3
  • There are many ways to do this so this is a rather open-ended question. Rather than trying to describe all the possible methods, maybe you can help narrow the question down a bit. e.g. You can run process as part of the system boot process (not tied to any specific user or login). There are also ways to tie it to a specific user (e.g. it will run when the user logs in). Does this program require a GUI? If you can provide a few details, we can probably point you to resources to guide you through a specific method likely to be useful to you. – Tim Campbell Mar 11 '19 at 17:26
  • The scrip I have made, mounts 2 drives from a wirless home server, and once connect it starts a slide show from some pictures on drives using a screensaver method. I needed there to be a delay between connecting to the wifi before mounting the drives, and a few seconds before it starts the slide show (to allow the mounting process to happen). – user46629 Mar 11 '19 at 17:32
  • I've tried using the ./superscript in the .bashrc (accessed using sudo nano .bashrc) I've also tried using the crontag by adding "@reboot /home/pi/startup.sh" at the end – user46629 Mar 11 '19 at 17:36
  • @user46629 You need to make sure that the problem is not within your script. Are you completely sure that the script isn't running on startup? – Benjamin Ashbaugh Mar 11 '19 at 18:59
  • @scitronboy yes I'm sure. – user46629 Mar 11 '19 at 19:00
  • I've had the system run untouched after reboot for an hour will no sign of the script doing anything. – user46629 Mar 11 '19 at 19:01
  • Add exec &> /home/pi/startup.log to your script print the output of your script to a logfile. – jake Mar 11 '19 at 22:25

1 Answers1

1

If your shell script runs from your command line as user pi, but DOESN'T run under cron, the two most likely reasons are:
1. Environment variables are different, typically the PATH: the cron user does not have the same environment as user pi does. You can easily overcome this by using a full path for all executables in your script.
2. Resources required in your script are not yet available when cron starts: cron does not know the status of services when it starts (for example, it doesn't know if the network is up). You can usually overcome this by using a brief sleep before executing your script.

You can help yourself by redirecting stderr for your script to a file. You currently have no visibility of error messages - a bit like driving with blinders on.

Also, make sure your script has an appropriate shebang line at the top so the system will know which interpreter to use.

Here's an example crontab entry that might work for you:

@reboot (/bin/sleep 30; /home/pi/startup.sh > /home/pi/cronjoblog 2>&1)

This will cause cron to sleep for 30 seconds before running your script. If you get any errors, they will be written to the file /home/pi/cronjoblog

Try this. Let us know if it still doesn't work, and we'll try to help you sort that.

Seamus
  • 21,900
  • 3
  • 33
  • 70
  • What do you mean my script has to have the appropriate "shebang" line? – user46629 Mar 13 '19 at 12:09
  • This is the commands my script has right is, "sudo mount -t cifs -o.....", "sudo mount -t cifs -o.....", "sleep 10", "feh -Y -x -q -D 5 -B black -F -Z -z -r........." – user46629 Mar 13 '19 at 12:18
  • Edit your question, please - we need to see your crontab; we can't do this via Comments. In the meantime, put this redirection after the command in your crontab: > /home/pi/cronjoblog 2>&1 – Seamus Mar 13 '19 at 12:50
  • I've only added 2 lines to the crontab. "0 0 * * * * root reboot" and "@reboot ( /bin/sleep 30; /home/pi/startup.sh > /home/pi/cronjoblog 2>&1)" – user46629 Mar 13 '19 at 12:57
  • Here's a google doc with the crontab and script. link – user46629 Mar 13 '19 at 13:59
  • @user46629: This really should go in your question - that's to your benefit, as more people who can help will see it. Simply click the edit link below your question, and add the information. – Seamus Mar 13 '19 at 17:01
  • Thank you. I didn't realize I could edit the question – user46629 Mar 13 '19 at 17:06