I have a Python script which runs in multiple instances with different parameters, for instance:

python3 proc.py -s -v -l proc_one.log
python3 proc.py -c -v -l proc_two.log

I usually start these in tmux sessions with a script on boot and manually check in on these, to restart if they crashed.

tmux new-session -d -s proct 'cd /opt/proct/ && python3 proc.py -s -v -l proc_one.log; bash'
tmux split-window -t proct 'cd /opt/proct/ && python3 proc.py -c -v -l proc_two.log; bash'

This is rather tedious and should be automated. I recently came across this solution, which offers kind of what I look for. A cronjob running the following bash script every couple of minutes:

if [[ ! $(pgrep -f '^python3 proc.py -s -v -l proc_one.log') ]]; then
    python3 proc.py -s -v -l proc_one.log

While this would keep my script running, it would also prevent me form checking in on the process.

My question is, how can I have a script check if my process is running, like the one above, but if it is not running, start it in a tmux session. I wouldn't mind separate tmux sessions for the different instances.


Make a service. Put


ExecStart=python proc.py --args ok




Also implement error handling so that the script does not crash.

| improve this answer | |
  • Interesting! Thanks for the suggestion. How can I monitor the stdout of the script if it runs as a service? – boolean.is.null Jun 18 at 19:28
  • I see the output can be viewed with journalctl, but it seems to only print the output, once the script exits. – boolean.is.null Jun 18 at 19:56
  • 2
    @boolean.is.null if output is not instant then it may be due to a buffer in your script. You can always redirect to a log file. – user1133275 Jun 19 at 19:55

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.