Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, I have created a bash script to run as non-root user. The script run well when executed from the CLI but not when scheduled as a cron job. Upon ...
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Location
    Mumbai, India
    Posts
    75

    [SOLVED] Script run in CLI but not in cron

    Hi,

    I have created a bash script to run as non-root user. The script run well when executed from the CLI but not when scheduled as a cron job. Upon investigating the cause of this issue I found that I have an variable setup in .bashrc ( EXPORT PYTHONPATH = list of directories). When the script is run as a cron job that variable is supposedly not set. I added that variable to the beginning of the scripts and it works fine when executed as a cron job.

    My query is why the variable does not get set for the user when the script is run as a cron job but works fine when executed from a CLI. I vividly remember that prior to server format I never had this variable a part of my script. It was only added to .bashrc of the non root user and would work fine even as a cronjob.

    Would appreciate assistance on the same

    --Syd

  2. #2
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    Hi,

    I hope that this will clarify things.
    Linux and me it's a love story

  3. #3
    Linux User
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    262
    Quote Originally Posted by syd05 View Post
    Hi,

    I have created a bash script to run as non-root user. The script run well when executed from the CLI but not when scheduled as a cron job. Upon investigating the cause of this issue I found that I have an variable setup in .bashrc ( EXPORT PYTHONPATH = list of directories). When the script is run as a cron job that variable is supposedly not set. I added that variable to the beginning of the scripts and it works fine when executed as a cron job.

    My query is why the variable does not get set for the user when the script is run as a cron job but works fine when executed from a CLI. I vividly remember that prior to server format I never had this variable a part of my script. It was only added to .bashrc of the non root user and would work fine even as a cronjob.

    Would appreciate assistance on the same

    --Syd
    The "~/.bashrc" is only run for interactive shells (unless you source it yourself).

  4. #4
    Just Joined!
    Join Date
    Dec 2009
    Posts
    8
    you can try to source the .bashrc from crontab:

    * * * * * . /home/foo/.bashrc; /home/foo/bin/myscript.sh

    or if that is not working you can create a wrapper script that sources .bashrc and then calls another script

    * * * * * /home/foo/bin/wrapper.sh /home/foo/bin/myscript.sh

  5. #5
    Just Joined!
    Join Date
    Aug 2009
    Location
    Mumbai, India
    Posts
    75

    RESOLVED - Script run in CLI but not in cron

    Hi,

    Thanx to all of you'll who have responded. Your replies gave me the reason for the script not working in cron and alternate options of getting the script initialized.

    Anyways, as I mentioned earlier, I don't recollect explicitly mentioning those variables in the script before the server was formatted. I'll try to figure that out later.

    Thanx.

    --Syd

Posting Permissions

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