我尝试写一个自定义的服务文件。
不幸的是我在journalctl中看不到输出。
这是我的单位档案:
root@om:~# cat /etc/systemd/system/ssh-tunnel-foo-de.service [Unit] Description=Tunnel For ssh-tunnel-foo-de After=network.target [Service] User=autossh ExecStart=/usr/bin/ssh -o "ExitOnForwardFailure yes" -o "ServerAliveInterval 60" -N -R 1080:localhost:1080 [email protected] ExecStartPre=-/usr/bin/ssh [email protected] "for pid in $$(ps -u tunnel | grep sshd| cut -d' ' -f1); do kill -9 $$pid; echo kill old ssh process $$pid; done" Restart=always RestartSec=5s StartLimitInterval=0 [Install] WantedBy=multi-user.target
Logoutput:
root@om:~# journalctl -u ssh-tunnel-foo-de Apr 01 10:12:28 om systemd[1]: Stopped Tunnel For ssh-tunnel-foo-de. Apr 01 10:12:28 om systemd[1]: Starting Tunnel For ssh-tunnel-foo-de... Apr 01 10:12:28 om systemd[1]: Started Tunnel For ssh-tunnel-foo-de. Apr 01 10:12:28 om systemd[1]: ssh-tunnel-foo-de.service: Main process exited, code=exited, status=217/USER Apr 01 10:12:28 om systemd[1]: ssh-tunnel-foo-de.service: Unit entered failed state. Apr 01 10:12:28 om systemd[1]: ssh-tunnel-foo-de.service: Failed with result 'exit-code'.
我怎样才能debugging出了什么问题。 我猜一些stderr输出会丢失。
你原来的问题是关于如何从systemd获得更多的输出。 看看如何debuggingsystemd单元ExecStart
但是,让我们看看我们是否无法让您的服务先行。
这引入了很多的复杂性,这是由一个包装脚本更好地处理。
[Unit] Description=Tunnel For ssh-tunnel-foo-de After=network-online.target [Unit] Description=Tunnel For ssh-tunnel-foo-de After=network-online.target [Service] User=autossh ExecStart=/home/autossh/bin/ssh-tunnel.sh start ExecStop=/home/autossh/bin/ssh-tunnel.sh stop PIDFile=/home/autossh/bin/ssh-tunnel.pid Restart=always RestartSec=5s StartLimitInterval=0 SuccessExitStatus=255 Type=forking [Install] WantedBy=multi-user.target
包装脚本将PID保存到PIDFile。 如果更改位置,则必须保持服务文件和包装器脚本同步。
您必须将“types”设置为分叉,以便systemd知道分叉正在发生。
SuccessExitStatus – 似乎进程死亡255 – 所以我们处理,以便停止的服务停止后不列为“失败”。
等到network-online.target启动后。 这意味着你实际上有一个networking连接,而不仅仅是一个networkingpipe理堆栈。 https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/ https://www.freedesktop.org/software/systemd/man/systemd.unit.html#
当然,你需要决定你真正想要的地方,并相应地修复path。
#!/bin/bash [email protected] usage () { echo "usage: $0 {start|stop}" exit } cd $HOME/bin case $1 in "start") /usr/bin/ssh -M -S socket-${MYHOST} -fnNT -o BatchMode=yes -o ExitOnForwardFailure=yes -o ServerAliveInterval=60 -R 1080:localhost:1080 ${MYHOST} EC=$? ; [ $EC -ne 0 ] && exit $EC PID=$(/usr/bin/ssh -S socket-${MYHOST} -O check socket-${MYHOST} 2>&1 | awk '/Master running \(pid=/ { sub(/^.+pid=/,"") ; sub(")","") ; print }') echo $PID > $HOME/bin/ssh-tunnel.pid ;; "stop") /usr/bin/ssh -S socket-${MYHOST} -O exit ${MYHOST} /bin/rm -f $HOME/bin/ssh-tunnel.pid exit 0 ;; *) usage ;; esac
查找ssh的一些参数。
“开始”部分设置隧道和“主”。 然后它向主机查询隧道的pid,并将其保存到一个pid文件中,以利于systemd。 如果以超级用户身份运行,则可以保存到/ var / run / * pid。
“停止”部分连接到主,并发出“退出”。 然后删除该pid文件。
User=autossh
是错误的。
这个用户不存在。
创build用户后,它的工作。
默认的Type=simple
很好。
不需要包装脚本。