标题: 如何让linux开机自动运行命令,且执行时指定目录运行
时间: 2022-09-05发布,2022-09-05修改
『回复列表(12|隐藏机器人聊天)』
cat << EOF | sudo tee /etc/systemd/system/hu60.service >/dev/null
[Unit]
Description=hu60
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=root
WorkingDirectory=/root
ExecStart=/root/main
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl start hu60
sudo systemctl enable hu60
sudo journalctl -u hu60
尝试2:
nohup sh -c "cd /root/main;sh" & //结果不运行
这是个啥。。cd
到一个文件?你会不会是想写:
nohup sh -c "cd /root/ && ./main" &
反正我在 Linux Deploy 里想自启应用,用的是 sysv,支持启动时切换 uid
gid
workdir
。自启脚本长这样(节选):
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg 'Starting' "$NAME"
/sbin/start-stop-daemon --start --quiet --oknodo --chuid "$USER:$GROUP" --pidfile "$PIDFILE" --make-pidfile --chdir "$WORKDIR" --exec "$DAEMON" --background -- "${DAEMONARGS[@]}"
log_end_msg $?
;;