Find the answer to your Linux question:
Results 1 to 7 of 7
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Bash script dont works when executed as cronjob


    Hello,

    i have cronjob:

    crontab -l
    Code:
    * * * * * pkill -f domexpcheck;sh /root/dom/domexpcheck.sh
    it runs:

    /var/log/cron
    Code:
    Mar 25 12:11:01 vps crond[17808]: (root) CMD (pkill -f domexpcheck;sh /root/dom/domexpcheck.sh)
    but somehow script dont run properly via cronjob. But when i execute cronjob command (pkill -f domexpcheck;sh /root/dom/domexpcheck.sh) from command line it works good... (i receive test email), via cron i dont receive and appears that script wont do the job

    stat /root/dom/domexpcheck.sh
    Code:
      File: `/root/dom/domexpcheck.sh'
      Size: 5542            Blocks: 16         IO Block: 4096   regular file
    Device: 17h/23d Inode: 23953953    Links: 1
    Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2014-03-24 23:46:21.000000000 +0100
    Modify: 2014-03-24 23:45:46.000000000 +0100
    Change: 2014-03-24 23:45:46.000000000 +0100
    whoami
    Code:
    root
    head /root/dom/domexpcheck.sh
    Code:
    [root@vps /]
    #pkill -f domexpcheck
    set -ex
    cd /root/dom
    please any ideas why it dont works via cron?
    "Avoid the Gates of Hell. Use Linux affordable VPS."

  2. #2
    Linux Guru
    Join Date
    Dec 2013
    Location
    Victoria, B.C. Canada
    Posts
    2,103
    You could add "2>/root/dom/error.txt" to the command line and capture error output.

  3. #3
    Linux Engineer docbop's Avatar
    Join Date
    Nov 2009
    Location
    Woodshed, CA
    Posts
    949
    cron runs a different shell unless you put a shbang in and specifiy what you want.
    #!/bin/bash

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Enthusiast Steven_G's Avatar
    Join Date
    Jun 2012
    Location
    Western US
    Posts
    599
    I always open all of my recurring system scripts with
    Code:
    #!/bin/bash -e
    and link them to run at start up through xfce4-session-settings (Session and start up). If I need it to run more than once during a session I use a simple sleep command to create a wait loop and then use
    Code:
    bash -e /full/path/to/script
    to restart the loop. And then close with exit 0. I cobbled together my own ubuntu remix from the minimal install and had to create several basic scripts to smooth over some of the rough spots, like some power mangement issues. They all work with no problem.

  6. #5
    thx, i tried Your advices, but dont works

    Code:
    * * * * * /usr/bin/pkill -f domexpcheck;sh /root/dom/domexpcheck.sh 2>/root/dom/error.txt
    no error file is created..

    part of beginning of the bash script domexpcheck.sh:

    Code:
    #!/bin/bash
    #export PATH=/usr/bin:/bin
    #pkill -f domexpcheck
    #set -ex
    echo "script running domexpcheck" | mail -s "script running" mymail@gmail.com
    cd /root/dom
    domlist=domlist
    also no mail is sent to me but crontab command is running, because when im working on script, it gets killed by above crontab command..

    Code:
    domlist=domlist
    domlist is file in /root/dom directory ...
    "Avoid the Gates of Hell. Use Linux affordable VPS."

  7. #6
    Linux Enthusiast Steven_G's Avatar
    Join Date
    Jun 2012
    Location
    Western US
    Posts
    599
    Code:
    #!/bin/bash
    #export PATH=/usr/bin:/bin
    #pkill -f domexpcheck
    #set -ex
    echo "script running domexpcheck" | mail -s "script running" mymail@gmail.com
    cd /root/dom
    domlist=domlist
    Why are lines 2,3&4 commented out? Don't you want them to run?

    The only line that you want to run that should have a "#" in it is the first one with "#!".

    And the full opening invoke command is
    Code:
    #!/bin/bash -e
    not
    Code:
    #!/bin/bash
    Have you looked at any of the begining guides to shell scripting that a lot of the gurus have in their signatures?

  8. #7
    When i removed "pkill -f domexpcheck;" from cronjob command, it started running, but it cant run when i use pkill before running the script, like its running simultaneously and gets killed.
    "Avoid the Gates of Hell. Use Linux affordable VPS."

Posting Permissions

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