[英]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.您好,我需要帮助来弄清楚如何在启动时运行 shell 脚本。 I know that the magicmirror.sh script works and I know it is in the right path.我知道 magicmirror.sh 脚本有效,而且我知道它走在正确的道路上。 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
.我已使用sudo crontab -e
将代码行添加到 crontab 的末尾。 The code is as follows.代码如下。
@reboot sleep 60 && /home/pi/magicmirror.sh
I think it's an issue with permissions.我认为这是权限的问题。 try use crontab -e without sudo.尝试使用不带 sudo 的 crontab -e。
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.由于 crontab 从非常小的 shell 运行,因此并非所有环境变量都被加载。 So maybe the $PATH
var is missing / not fully loaded, so some binary you call from inside the script is not found.因此,可能$PATH
变量丢失/未完全加载,因此找不到您从脚本内部调用的某些二进制文件。
I guess you script starts with #!/bin/bash
.我猜你的脚本以#!/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.如果我的猜测是正确的,请尝试使用#!/bin/env /bin/bash
启动脚本(这意味着“使用完整环境启动 bash”)或将脚本中的所有调用/命令替换为完整路径。 eg /usr/local/bin/myprog dosomething
instead of myprog dosomething
.例如/usr/local/bin/myprog dosomething
而不是myprog dosomething
。
EDIT: As @shv mentioned this could ALSO be a problem of permissions.编辑:正如@shv 提到的,这也可能是权限问题。 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.如果您运行sudo crontab -e
您正在编辑root
的 crontab,而不是您的pi
用户。 This has 2 effects:这有两个效果:
root
has to interact in a different way with GPIO than your pi user.我可以想象,尽管对 GPIO 有更多权限,但root
必须以不同于 pi 用户的方式与 GPIO 交互。 (wild guess) (胡乱猜测)magicmirror.sh
at will and write anything it it.您的 pi 用户可以随意编辑magicmirror.sh
并在其中写入任何内容。 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 eg rm -rf --no-preserve-root /
and just rebooting the device, to clean the filesystem.因此,例如,有权访问该 pi 用户的人(因为他“入侵”了系统,或者只是您授予访问权限的人)可以编写例如rm -rf --no-preserve-root /
并重新启动设备, 清理文件系统。 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) eg /root/magicmirror.sh
要解决此问题,您可以使用crontab -e
(不带 sudo)编辑您自己的 crontab,或者将该脚本放在只有 root 可以访问的地方(如果您需要 root 权限),例如/root/magicmirror.sh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.