定制服务journactl没有错误消息

我尝试写一个自定义的服务文件。

不幸的是我在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

但是,让我们看看我们是否无法让您的服务先行。

关于systemd

有些问题

  1. ssh在启动后分叉
  2. 一个SSH隧道应该在后台运行
  3. 一个systemd进程默认运行的types是“simple”,它预计StartExec中的命令是主服务 (参见“1”)
  4. 为了让systemd知道你的服务是否存在,大量的时间都需要PIDFile。 GuessMainPID默认为yes,但也可能是ssh,误以为是。
  5. 你在当地杀死隧道 – 无需用剑出国

这引入了很多的复杂性,这是由一个包装脚本更好地处理。

/etc/systemd/system/ssh-tunnel-foo-de.service

 [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 

对systemd的解释

包装脚本将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#

包装脚本:

/home/autossh/bin/ssh-tunnel.sh

当然,你需要决定你真正想要的地方,并相应地修复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的一些参数。

  • -M和-Sbuild立一个主连接和一个命名的Socket来控制主。
  • f – 转到背景
  • n – 防止从stdin读取(由-f指示)
  • N – 没有远程命令
  • T – 禁用伪tty分配

“开始”部分设置隧道和“主”。 然后它向主机查询隧道的pid,并将其保存到一个pid文件中,以利于systemd。 如果以超级用户身份运行,则可以保存到/ var / run / * pid。

“停止”部分连接到主,并发出“退出”。 然后删除该pid文件。

致谢https://stackoverflow.com/a/15198031/2045924

User=autossh是错误的。

这个用户不存在。

创build用户后,它的工作。

默认的Type=simple很好。

不需要包装脚本。