StasiewiczJacob
StasiewiczJacob

Reputation: 71

how to run a shell script on startup raspberry pi?

Hello I need help trying to figure out how to run a shell script in startup. I know that the magicmirror.sh script works and I know it is in the right path. When I reboot my raspberry pi it does not run the script. I have added the line of code to the end of the crontab using sudo crontab -e. The code is as follows.

@reboot sleep 60 && /home/pi/magicmirror.sh

Upvotes: 0

Views: 2428

Answers (2)

Malik
Malik

Reputation: 904

The problem could be the way you call commands in your script. As the crontab runs from e very minimal shell, not all environment vars get loaded. So maybe the $PATH var is missing / not fully loaded, so some binary you call from inside the script is not found.

I guess you script starts with #!/bin/bash. If my guess is correct either try to start te script with #!/bin/env /bin/bash (this means "start bash with a full environment") or replace all calls/commands in you script with their full path. e.g. /usr/local/bin/myprog dosomething instead of myprog dosomething.

EDIT: As @shv mentioned this could ALSO be a problem of permissions. But maybe in a different kind of way. If you run sudo crontab -e you are editing the crontab of root, not of your pi user. This has 2 effects:

  1. you script is run in the environment of root. Sometimes some configurations enable you to do things differently than the root user. I am by no means a Raspberry pi expert, but it could be that your magic mirror accesses some GPIO pins. I can imagine that, although having more permissions to GPIO, root has to interact in a different way with GPIO than your pi user. (wild guess)
  2. This is actually a security risk. Your pi user can edit the magicmirror.sh at will and write anything it it. So for example, someone having access to that pi user (either because he "hacked" the system, or is just someone you gave access to it) could write e.g. rm -rf --no-preserve-root / and just rebooting the device, to clean the filesystem. And you would not want to to that, do you? To fix this you can either just edit you own crontab with crontab -e (without sudo) or put that script somewhere only root can access (if you need the root permissions) e.g. /root/magicmirror.sh

Upvotes: 1

shv
shv

Reputation: 57

I think it's an issue with permissions. try use crontab -e without sudo.

Upvotes: 0

Related Questions