Run a persistent process via ssh

If you're SSHing to a Linux distro that has systemd, you can use systemd-run to launch a process in the background (in systemd's terms, "a transient service"). For example, assuming you want to ping something in the background:

systemd-run --unit=pinger ping 10.8.178.3

The benefit you'll get with systemd over just running a process with nohup is that systemd will track the process and its children, keep logs, remember the exit code and allow you to cleanly kill the process and all its children. Examples:

See the status and the last lines of output:

systemctl status pinger

Stream the output:

journalctl -xfu pinger

Kill:

systemctl kill pinger

As an alternative to nohup, you could run your remote application inside a terminal multiplexor, such as GNU screen or tmux.

Using these tools makes it easy to reconnect to a session from another host, which means that you can kick a long build or download off before you leave work and check on its status when you get home. For instance. I find this particularly useful when doing development work on servers that are very remote (in a different country) with unreliable connectivity between me and them, if the connection drops, I can simply reconnect and carry on without losing any state.

Tags:

Linux

Unix

Ssh