Results 1 to 9 of 9

Thread: Python Script only works when run from terminal, not from cron

  1. #1
    Join Date
    Jul 2012
    Location
    Buffalo, NY
    Beans
    58
    Distro
    Kubuntu 22.04 Jammy Jellyfish

    Question Python Script only works when run from terminal, not from cron

    Edit I want to clarify that the path is not the problem. Other commands within the python script execute just fine. It's specifically the nmcli radio wifi ... commands that don't do anything.

    Kubuntu 22.04

    I wrote a python script to reset my wifi connection. It does this by sending to the shell: nmcli radio wifi off, waiting 6 seconds and then sending nmcli radio wifi on.


    The actual line in the Python script:
    Code:
    subprocess.run('nmcli radio wifi off'.split(' '))

    If I run the script manually from a terminal it works fine and it dosen't require sudo or anything like that. But if the script runs as a cron job, then it seems that those commands don't get executed; the wifi doesn't get turned off. Any ideas why? Is there some weird permission thing happening that's different from me as a logged-in user and the cron jobs that belong to my user account?
    Last edited by AwaitingUserInput; January 5th, 2023 at 11:03 PM.

  2. #2
    Join Date
    Aug 2016
    Location
    Wandering
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: Python Script only works when run from terminal, not from cron

    This is something that I'll forget to do,>>> added the statement path to command the top of crontab.
    Based off some notes I've kept, The default PATH for cron jobs is just /usr/bin:/bin, so if you want to run
    anything outside of those directories you need to either use an explicit path
    to the binary, or set the PATH variable in the crontab file.
    Last edited by 1fallen; January 5th, 2023 at 10:38 PM. Reason: corrected wording
    With realization of one's own potential and self-confidence in one's ability, one can build a better world.
    Dalai Lama>>
    Code Tags | System-info | Forum Guide lines | Arch Linux, Debian Unstable, FreeBSD

  3. #3
    Join Date
    Oct 2005
    Location
    Lab, Slovakia
    Beans
    10,804

    Re: Python Script only works when run from terminal, not from cron

    With cron, it is best to use the full pathname for every command called.

  4. #4
    Join Date
    Aug 2016
    Location
    Wandering
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: Python Script only works when run from terminal, not from cron

    Quote Originally Posted by HermanAB View Post
    With cron, it is best to use the full pathname for every command called.
    +1
    This may help if your script still fails to execute: https://gist.github.com/alberthdev/8...0655a3d9f75f4c
    With realization of one's own potential and self-confidence in one's ability, one can build a better world.
    Dalai Lama>>
    Code Tags | System-info | Forum Guide lines | Arch Linux, Debian Unstable, FreeBSD

  5. #5
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Python Script only works when run from terminal, not from cron

    Quote Originally Posted by AwaitingUserInput View Post
    If I run the script manually from a terminal it works fine and it dosen't require sudo or anything like that. But if the script runs as a cron job, then it seems that those commands don't get executed; the wifi doesn't get turned off. Any ideas why? Is there some weird permission thing happening that's different from me as a logged-in user and the cron jobs that belong to my user account?
    Just to extend what others already said. processes run under cron have a minimal environment. This is very different from a full session environment, like a user who logs in gets. BTW, this bites everyone. Cron doesn't run the initialization files like login does, so your ~/.profile and ~/.bashrc settings aren't picked up for any crontab program.

    To see the differences, put 1 command into your crontab and run the same command from a shell. Just have the crontab output be redirected to /tmp/file.cron and the shell output redirected to /tmp/file.shell.

    Now use any diff tool you like, 'meld' is the best I know, and compare /tmp/file.cron to /tmp/file.shell. See all the differences? That's likely why the cron job fails. Of course, YMMV.

    +1 for using the full path to any program called. Never trust the PATH in any script. That's a good way to have unexpected results.

  6. #6
    Join Date
    Aug 2016
    Location
    Wandering
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: Python Script only works when run from terminal, not from cron

    Quote Originally Posted by TheFu View Post
    Never trust the PATH in any script. That's a good way to have unexpected results.
    Exactly, I used "PATH" as a point to, so I'll correct that in my first reply, I can see that would be confusing. (Thanks for calling that out)
    With realization of one's own potential and self-confidence in one's ability, one can build a better world.
    Dalai Lama>>
    Code Tags | System-info | Forum Guide lines | Arch Linux, Debian Unstable, FreeBSD

  7. #7
    Join Date
    Jul 2012
    Location
    Buffalo, NY
    Beans
    58
    Distro
    Kubuntu 22.04 Jammy Jellyfish

    Re: Python Script only works when run from terminal, not from cron

    Thanks, but the issue is not the path in the crontab. I already have an absolute path to the Python script in crontab. And there are other bash/shell commands within the Python script that run just fine. It's only turning off the wifi connection that doesn't work.

    These shell commands work OK even when cron is the one running the script:

    • iperf3 to measure network speed
    • ps aux to check if rsync is running
    • sending text to /dev/pts/0 to warn me that the network is about to get reset


    All of these work OK, it's just the nmcli commands that get skipped when running the script via cron. If I manually run it from a terminal, everything works.


    Here is a piece of the Python script though you don't really need it to understand the problem that I'm having.
    Code:
    def reset_network(speed):
        message = (datetime.datetime.now().strftime('%a %H:%M: ') + f'Network is {speed} MB/s. Restarting in 15 seconds.').encode()
        
        pts = os.open('/dev/pts/0', os.O_RDWR)
        os.write(pts, message)
        time.sleep(15)
        subprocess.run('nmcli radio wifi off'.split(' '))
        time.sleep(6)
        subprocess.run('nmcli radio wifi on'.split(' '))
        os.close(pts)

  8. #8
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Python Script only works when run from terminal, not from cron

    subprocess.run('nmcli radio wifi off'.split(' '))

    That isn't using the full path to the program. nmcli needs to be /usr/bin/nmcli --- or something like that

    The web-based manpage does say something about needing access to GConf, which holds keys needed to do things. Under cron, those keys aren't available. Check your local manpage about this to be certain. Don't trust web manpages, since they aren't likely to be for the same version of the program you have installed.

    Rather than using a workaround, why not just setup netplan to bring up the wifi interface system-wide and solve any issues with that directly?

  9. #9
    Join Date
    Aug 2016
    Location
    Wandering
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: Python Script only works when run from terminal, not from cron

    See if this helps:https://askubuntu.com/questions/1294...fi-automaticly
    It's pretty detailed with options.
    mines just a basic:
    Code:
    import os
    import time
    from datetime import datetime
    import sys
    
    while(1):
        now = datetime.now()
        if now.hour == 0:
            print("Starting Wifi...\n")
            os.system("nmcli radio wifi on")
        if now.hour == 6:
            print("Turning Wifi off...\n")
            os.system("nmcli radio wifi off")
            sys.exit()
        time.sleep(60)
    With realization of one's own potential and self-confidence in one's ability, one can build a better world.
    Dalai Lama>>
    Code Tags | System-info | Forum Guide lines | Arch Linux, Debian Unstable, FreeBSD

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •