1

I am new to scheduling tasks with crontab. I am trying to open python script as if I open player and play some song. Programm work well alone, but with crontab its did not work. my crontab task

*/5 * * * * cd /home/jack/projects/auto_stuff/18 && /usr/bin/python3.6 torrent_control.py

my code for open player and play song

with open('song.txt', 'a') as f:
    try:
        if player == 'play':
            subprocess.Popen(['/usr/bin/deadbeef', 'song.mp3'])
    except Exception:
        traceback.print_exc(file=f)

I know before subprocess code works right. I seen advice absolute path in "subprocess.Popen", but its already absolute or Im wrong? I try to log my outputs to a file and script does not show any exceptions. I change crontab task

*/5 * * * * /usr/bin/python3.6 /home/jack/projects/auto_stuff/18/torrent_control.py

And I change script code:

with open('/home/jack/projects/auto_stuff/18/song.txt', 'a') as f:
    try:
        if player == 'play':
            subprocess.Popen(['/usr/bin/deadbeef', '/home/jack/projects/auto_stuff/18/song.mp3'])
            f.write('  Done.\n')
    except Exception:
        traceback.print_exc(file=f)
        print('Not done', file=f)

in song.txt output always 'Done.'

dessand
  • 11
  • 3
  • Does this answer your question? [Execute Python script via crontab](https://stackoverflow.com/questions/8727935/execute-python-script-via-crontab) – Tanishq Vyas Dec 17 '20 at 11:02
  • Where is song.mp3? – Raman Sailopal Dec 17 '20 at 11:07
  • Try to log outputs from your python scripts to a file, to check if the script is executing or there is an error in your script. You can wrap your code in try...except to log the exception to the file. – ace_racer Dec 17 '20 at 12:59
  • @RamanSailopal song mp3 in same directory as .py file /home/jack/projects/auto_stuff/18 – dessand Dec 17 '20 at 14:34
  • Try to not use cd. Instead use the full path to the py file in crontab and also reference the full path to the mp3 in your Python script. – Raman Sailopal Dec 17 '20 at 14:35
  • @ace_racer I try to log my outputs to a file and script does not show any exceptions. – dessand Dec 17 '20 at 14:39
  • @dessand Try to log some informational messages to the log file at the beginning of the script to check if the python script was triggered by cron – ace_racer Dec 17 '20 at 15:09
  • @RamanSailopal I change crontab task, and again any exception, script work but the program does not open. – dessand Dec 17 '20 at 15:32

0 Answers0