我想让 sslocal 命令自动启动,所以我在 /etc/rc.local 中写入
sudo sslocal -c /etc/ss.json -d start
plank
exit 0
但重启后,sudo systemctl status rc-local
显示
rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset: enabled)
Drop-In: /lib/systemd/system/rc-local.service.d
└─debian.conf
Active: failed (Result: exit-code) since 2017-12-07 16:05:51 +08; 6min ago
Process: 1155 ExecStart=/etc/rc.local start (code=exited, status=1/FAILURE)
December 07 16:05:46 lucas-CW65S rc.local[1155]: INFO: loading config from /etc/ss.json
December 07 16:05:50 lucas-CW65S rc.local[1155]: 2017-12-07 16:05:50 INFO loading libcrypto from libcrypto.so.1.0.0
December 07 16:05:50 lucas-CW65S sudo[1158]: pam_unix(sudo:session): session closed for user root
December 07 16:05:51 lucas-CW65S rc.local[1155]: Failed to connect to Mir: Failed to connect to server socket: no file and directory
December 07 16:05:51 lucas-CW65S rc.local[1155]: Unable to init server: cannot connect : deny connect
December 07 16:05:51 lucas-CW65S rc.local[1155]: [WARN 16:05:51.155503] [Gtk] cannot open display:
December 07 16:05:51 lucas-CW65S systemd[1]: rc-local.service: Control process exited, code=exited status=1
December 07 16:05:51 lucas-CW65S systemd[1]: Failed to start /etc/rc.local Compatibility.
December 07 16:05:51 lucas-CW65S systemd[1]: rc-local.service: Unit entered failed state.
December 07 16:05:51 lucas-CW65S systemd[1]: rc-local.service: Failed with result 'exit-code'
关键点可能是sudo[1158]: pam_unix(sudo:session): session closed for user root
切换到 sudo 状态时似乎需要 sudo 密码,但是我如何在 rc.local 文件中输入它?
答案1
你那里有两个问题。
/etc/rc.local
以 root 身份运行,因此你不需要sudo
rc.local
&
是一个脚本,这些命令是连续的,每个命令都会阻止执行。在末尾添加可在后台运行命令。
还要注意, rc.local
使用#!/bin/sh -e
表示它会在任何错误时退出。如果您希望即使其中一个命令失败,脚本仍继续执行,请使用command arg1 arg2 || true
类型的方法。
答案2
简单修复:从脚本中删除单词“sudo”。
原因:/etc/rc.local
已经以 root 身份运行,因此您不需要sudo
那里。
PS:无论出于什么原因,您都不应该sudo
从 shell 脚本进行调用。