我有一个使用 msqllib 的程序。它扫描我构建的一些硬件,然后更新 mysql 数据库。这个程序已经运行了很多年,但我从来没有能够在系统重新启动时自动启动它。现在我遇到了一个我无法控制的电源循环问题,所以想让它自动运行。
在 rc.local 中:我有:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
/home/nigel/scan
exit 0
现在,当我在命令提示符下输入代码并更新我的数据库时,代码运行良好。但是,当我把它放在 rc.local 中时, ps -ef
显示:
root 356 350 0 20:54 ? 00:00:03 /home/nigel/scan
但是程序没有执行更新数据库的 SQL 调用。
如果我询问 rc.local 的状态:
root@Pi-Scan:~# systemctl status rc-local.service
● rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled)
Drop-In: /usr/lib/systemd/system/rc-local.service.d
└─debian.conf
/etc/systemd/system/rc-local.service.d
└─ttyoutput.conf
Active: activating (start) since Sat 2023-05-06 20:54:19 EDT; 12min ago
Docs: man:systemd-rc-local-generator(8)
Cntrl PID: 350 (rc.local)
Tasks: 2 (limit: 414)
CPU: 3.903s
CGroup: /system.slice/rc-local.service
├─350 /bin/sh -e /etc/rc.local start
└─356 /home/nigel/scan
May 06 20:54:21 Pi-Scan rc.local[356]: error: first parameter is not a valid address family: Transport endpoint is not connected
May 06 20:54:22 Pi-Scan rc.local[356]: error: first parameter is not a valid address family: Transport endpoint is not connected
May 06 20:54:23 Pi-Scan rc.local[356]: error: first parameter is not a valid address family: Transport endpoint is not connected
May 06 20:54:24 Pi-Scan rc.local[356]: error: first parameter is not a valid address family: Transport endpoint is not connected
May 06 20:54:25 Pi-Scan rc.local[356]: error: first parameter is not a valid address family: Transport endpoint is not connected
May 06 20:54:26 Pi-Scan rc.local[356]: error: first parameter is not a valid address family: Transport endpoint is not connected
May 06 20:54:27 Pi-Scan rc.local[356]: error: first parameter is not a valid address family: Transport endpoint is not connected
May 06 20:54:28 Pi-Scan rc.local[356]: error: first parameter is not a valid address family: Transport endpoint is not connected
但是,如果我发出systemctl restart rc-local.service
该程序执行第二个实例并且工作正常!
考虑到这可能是赛车条件,我尝试在命令前添加一个 sleep 5,但仍然遇到同样的问题。