Results 1 to 8 of 8
  1. #1
    Lounger
    Join Date
    Dec 2009
    Location
    Burlington, VT
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post

    Scheduling a Python job in Linux

    I'm trying to schedule a python script to run via crontab and it's not working. This is on Citrix's XenServer platform which is Linux.

    The script runs fine from the command prompt, but when I run it via cron i get an error:
    Code:
    File "./sshkill.py", line 3
    ps -u root -f | grep "ssh grpadmin@192.168.200.20" | while read psline; do
    ^
    SyntaxError: invalid syntax
    The script is:
    Code:
    #!/bin/sh
    ps -u root -f | grep "ssh grpadmin@192.168.200.20" | while read psline; do
    pid=`echo $psline | awk '{print $2}'`
    ppid=`echo $psline | awk '{print $3}'`
    if test $ppid -eq '1' ; then
    kill $pid
    fi
    done
    and the crontab entry I have is:
    Code:
    2 * * * * /usr/bin/python /usr/bin/sshkill.py > /tmp/sshkill.txt 2>&1
    Any ideas?

  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,207
    Thanks
    49
    Thanked 989 Times in 919 Posts
    Does Cron support redirect ?

    cheers, Paul

  3. #3
    5 Star Lounger
    Join Date
    Jan 2010
    Location
    Los Angeles, CA
    Posts
    828
    Thanks
    4
    Thanked 38 Times in 34 Posts
    What you posted is not a python script, it is a shell script (that is what the "#!/bin/sh" on the first line means). No wonder python doesn't like it.

  4. #4
    Lounger
    Join Date
    Dec 2009
    Location
    Burlington, VT
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    I had pulled the script from a forum posting, and a different post in the thread mentioned something about python, so I assumed...

    That being said, I still get the error, even changing the crontab entry to /bin/bash. Other posts in the thread say they've gotten it running, so I must be missing something.

  5. #5
    5 Star Lounger
    Join Date
    Jan 2010
    Location
    Los Angeles, CA
    Posts
    828
    Thanks
    4
    Thanked 38 Times in 34 Posts
    Try this: change the name of the file to sshkill and then change your cron command to:
    Code:
    2 * * * * /usr/bin/sshkill > /tmp/sshkill.txt 2>&1
    Also, make sure that your script is executable. You can do this as follows (signed in as root):

    Code:
    chmod a+x /usr/bin/sshkill
    Then to test the command to make sure it runs, from the command prompt enter:

    Code:
    sshkill
    Only once the command works from the command line can you expect it to run via cron.

  6. The Following User Says Thank You to cafed00d For This Useful Post:

    MikeBinVT (2011-03-31)

  7. #6
    Lounger
    Join Date
    Dec 2009
    Location
    Burlington, VT
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    Script runs from command prompt fine. I gave up for a little while and when I came back it had run at 11:02. I was trying to schedule it to run every 2 minutes, but it seems to be running at 2 minutes past the hour?

  8. #7
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,207
    Thanks
    49
    Thanked 989 Times in 919 Posts
    You have specified minute 2 of every hour on every day etc. Try this - all minutes with an interval of 2.
    0-59/2 * * * *

    cheers, Paul

  9. The Following User Says Thank You to Paul T For This Useful Post:

    MikeBinVT (2011-03-31)

  10. #8
    Lounger
    Join Date
    Dec 2009
    Location
    Burlington, VT
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    That's got it! Thanks all.

Posting Permissions

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