我rtc.service
在以下地区提供服务/etc/systemd/system
:
[Unit]
Description=RTC Service
[Install]
WantedBy=multi-user.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/scripts/script_rtc.sh
但script_rtc.sh
存在并且可执行:
[user@comp001 scripts]# ls -asl script_rtc.sh
4 -rwxr-xr-x 1 root root 79 Jan 1 01:04 script_rtc.sh
[user@comp001 scripts]#
这是脚本/scripts/script_rtc.sh
,仅通过 i2c 启用实时时钟:
[user@comp001 scripts]# cat script_rtc.sh
#!/bin/sh
sudo echo "ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device"
[user@comp001 scripts]#
这里还有systemclt -u rtc.service
输出:
Jan 01 01:00:10 comp001 systemd[1]: Starting RTC Service...
Jan 01 01:00:12 comp001 systemd[1]: rtc.service: main process exited, code=exited, status=203/EXEC
Jan 01 01:00:12 comp001 systemd[1]: Failed to start RTC Service.
Jan 01 01:00:12 comp001 systemd[1]: Unit rtc.service entered failed state.
Jan 01 01:00:12 comp001 systemd[1]: rtc.service failed.
好的,我已经修改了脚本本身以测试回显:
#!/bin/sh
echo "ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device"
现在,我可以通过 控制它systemctl
,但如果我这样做:
#!/bin/sh
echo "ds1307 0x68" > /sys/class/i2c-adapter/i2c-1/new_device
I get error:
[root@comp001 scripts]# ./script_rtc.sh
./script_rtc.sh: line 3: echo: write error: Invalid argument
[root@comp001 scripts]#
为什么??
答案1
不管怎样,脚本都无法执行。
- 尝试从 shell 启动它
- 检查
noexec
、SELinux 等(无论您可能有什么安全限制) - 检查脚本第一行中的 shebang(# 之前的空格,! 之前的空格,验证是否存在
/bin/sh
)
顺便一提:
sudo
从脚本中删除,它已经以 root 身份运行- 重定向不应被引用,即
echo "foo" > file
,不echo "foo > file"
然而
无法保证您的脚本会被执行后 i2c-1
已初始化,所以你有竞争条件这里。你最好使用 udev 规则而不是完全使用脚本:
ACTION=="add", SUBSYSTEM=="i2c", ATTR{name}=="<contents of file /sys/class/i2c-adapter/i2c-1/name>", ATTR{new_device}="ds1307 0x68"
将该行放入/etc/udev/rules.d/99-i2c-rtc.rules
.
答案2
回声应该是:
echo "ds1307 0x68">/sys/class/i2c-adapter/i2c-1/new_device
没有空格......这对我有用。永远不要忘记:
systemctl enable <your.service>...