0

My config:

debian 10.9

4.19.0-19-amd64

OpenSSH_7.9p1 Debian-10+deb10u2, OpenSSL 1.1.1d 10 Sep 2019

autossh 1.4g

GNU bash, 5.0.3(1)-release (x86_64-pc-linux-gnu)

I start a service in /etc/systemd/system/myshh.service

ExecStart=/home/user/ssh_run starts my script ssh_run

THIS ARE ONLY TWO OF A LOT EXAMPLES WITH SSH AND AUTOSSH I TRIED

ssh_run script

#!/bin/bash

ssh -f -NT -o "ServerAliveInteval=30" -o "ServerAliveCountMax=2" \
-R 5555:localhost:443 -l [USER] [IP] -p [PORT] -i [KEY-WITHOUT-PASSWORD]

EXECUTE NEXT COMMAND

or

#!/bin/bash

/usr/bin/autossh -f -NT -o "ExitOnForwardFailure=yes" \
-R 5555:localhost:443 -l [USER] [IP] -p [PORT] -i [KEY-WITHOUT-PASSWORD]

EXECUTE NEXT COMMAND
  1. When i execute the command alone from the shell it works ssh -f -NT ......

  2. When i execute only the script it works ./ssh_run

  3. When i start the service /etc/systemd/system/myshh.service without -f it works

ALL 3 WITHOUT EXECUTE OF THE NEXT COMMANDS

PROBLEM:

When i start the service /etc/systemd/system/myshh.service, with the script ./ssh_run and i use -f for the background, now the next commands execute but, service exited with status code 0 but no ssh or autossh in ps aux | grep [s]sh or ps aux | grep [a]utossh

Checked systemctl status myshh.service i grep the journalctl too without an error

the service stops or restart with the same result, i try with and without Restart=30, Restart=always or Environment="AUTOSSH_GATETIME=0" in the service

I read most of the posts of all the stacksites, and try the search engines, no solution found i try (command) , exec, bash -c .... without result...

Now my problem/question:

How can i execute a ssh/autossh remote port forwarding command inside a bash script in the background, that is start from a service, to execute the next commands.

1 Answer 1

0

If someone has the same problem, this post save my day now:

How do I figure out why my systemctl service didn't start

Add Type=forking to my service file /etc/systemd/system/myshh.service.

Now i can execute a ssh/autossh remote port forwarding command inside a bash script and the -f option for the background execution, that is start from a service, to execute the next commands/ more commands from/in a script.

Other links to posts with relevant informations:

How to see full log from systemctl status service?

Difference between nohup, disown and &

Start a background process from a script and manage it when the script ends

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .