0

I've wrote a little script to launch a program and send a mail if a program has quit:

#!/usr/bin/env python
# coding: utf8

import psutil    
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
import time
import os

program = "transmission-gtk"
if not program in [psutil.Process(i).name for i in psutil.get_pid_list()]:
   try:
      os.system(program)
      text = "but it has been restarted" 
   except IOError as e:
      text = "The restart failed!\n" + e

   time.sleep(2)

   msg = MIMEText("Transmission has been closed !!\n" + text)
   msg["From"] = "[email protected]"
   msg["To"] = "[email protected]"
   msg["Subject"] = "subject"
   p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
   p.communicate(msg.as_string())

The script is launched by cron, every 15 min. All works, except one thing: transmission quit at the same moment the script quits...

I don't want to write a permanent script (raspberry-pi), so a while loop is not what I want.

Then, how to launch my program ?

1 Answer 1

1

You may use os.system(program+'&') to background the execution of your program.

Also, even if your program works, you should have a look at the subprocess module (which intends to replace os.system(), among other things).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response. It was just a problem of Display and environment variable for cron... Anyway, all solutions for this question can be found here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.