1

I am working on Martin F. O'Connor's internet connection monitor (https://github.com/mfoc/monitor-internet-connection), which records Internet Outages.

The script runs from the command line ($python3 -m monitor-internet-connection) and outputs any activity to the terminal screen in real time.

Closing the ssh session closes the script. Upon closing, the script writes the activity that was output to the ssh screen to a log file.

My objective is to be able to have the script run when the ssh session terminates (i.e., not close when the terminal is closed).

1
  • nohup python3 -m monitor-internet-connection any output will go to ./nohup.out in the current wroking directory. – Dougie Nov 14 '20 at 1:01
2

I think there are many ways to do this. Perhaps the "most correct" way is to daemonize the app. There's an answer on Stack Overflow that provides some suggestions. You could do that yourself, or perhaps consider submitting that request to the author via creating a new issue on his GitHub page.

Until that happens:

  1. Have you tried just running it in the background?
$ python3 -m monitor-internet-connection &
  1. You could start the program as a cron job: @reboot python3 ...

  2. If you're familiar with screen or tmux that's another way to do this... there's a Q&A here that shows how. This approach has the advantage that you can restore it.

  3. Maybe nohup?:

$ nohup python3 -m monitor-internet-connection

What it seems you want to do is detach this program from your terminal session. If you search something like linux run script and detach from terminal, you will find some other choices I think.

2
  • Thank you, Seamus. I tried all of your suggestions, so I appreciate the learning experience. #3 -- use screen or tmux -- turned out to be the best solution, precisely for the reason you suggested. BTW, I was unfamiliar with GitHub issue process ( after reading the response(s) to the one issue posted, it seemed rather condescending). I unsuccessfully had tried other means to contact the author. – Brokenstick Nov 14 '20 at 20:35
  • @Brokenstick: RE: 'condescending': Don't be put off by that BS. Years ago, when I was initially exploring the "Unix-sphere", I tried OpenBSD. The project leader is a horrible asshole, but in all fairness, he did get things done. My point is to take it with a grain of salt, and don't let anyone discourage you from learning. – Seamus Nov 14 '20 at 21:29
1

A basic way to do this would be to use screen or tmux.

Closing the ssh session closes the active terminal but does not end the screen session, and you can later reattach using screen -x or tmux attach.

If you want to get more serious about things you could write a simple systemd service to start the script at boot and keep it running in the background. You'd just need to have your script write to a file (perhaps just redirecting the output?) instead of stdout.

1
  • screen turned out to be the best solution. I have started looking at systemd service for another project; I like having the ability to find multiple solutions. Thank you. – Brokenstick Nov 14 '20 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.