I have created the following systemd service to automatically keep an autossh tunnel open between two servers:
#Systemd unit file for autossh
[Unit]
Description=Autossh Tunnel
[Service]
Type=forking
ExecStart=/usr/bin/autossh -M 20009 -f -N -L 3307:127.0.0.1:3306 remoteServer
ExecStop=pkill autossh
User=ubuntu
Group=ubuntu
UMask=0007
RestartSec=1
Restart=always
[Install]
WantedBy=multi-user.target
The file is loacted in /etc/systemd/system/autossh-tunnel.service
When I run it I get the following error message:
ubuntu@verifier:~/.ssh$ sudo systemctl daemon-reload
ubuntu@verifier:~/.ssh$ sudo service autossh-tunnel start
Failed to start autossh-tunnel.service: Unit autossh-tunnel.service is not loaded properly: Exec format error.
See system logs and 'systemctl status autossh-tunnel.service' for details.
ubuntu@verifier:~/.ssh$ systemctl status autossh-tunnel.service
● autossh-tunnel.service - Autossh Tunnel
Loaded: error (Reason: Exec format error)
Active: inactive (dead)
which autossh
yields:
ubuntu@verifier:~/.ssh$ which autossh
/usr/bin/autossh
I have also tried to run the exact same command /usr/bin/autossh -M 20009 -f -N -L 3307:127.0.0.1:3306 remoteServer
in the console and I did not have any problems. However, as a systemd service it fails.
What is wrong here? I have another server with the same setup and there this script runs without any issues. So I do not understand what is going on here...
Quick answer: Make sure to check the output of
/var/log/syslog
for detailed error messages.Long answer: Okay, I found out what the problem is.
Shortly after calling
sudo service autossh-tunnel start
I check the output insudo tail -n 200 /var/log/syslog
and I got the output:So I changed the service file to call
/usr/bin/pkill
instead of justpkill
.That did solve the prolem. Now
sudo tail -n 200 /var/log/syslog
yields:And a
ps aux | grep autossh
also shows me that it is running.