Thread: Crontab

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Overseas - Azores
    Posts
    7
    Rep Power
    0

    Question Crontab


    Hello. Bare with me, I am still learning UNIX. So, you may laugh at the simplicity of my questions.

    I'd like to know a few things that I have not been able to find online. I may have just not found the right spot yet. But, a lot of my searching ends up in forums. So, I thought I might as well just start a thread.

    I have been fooling around with crontab. It seems very simple according to the man pages; however, one of my entries will not work. I know I have the 5 entries for the duration the command is executed set correctly...and I know I haven't fat-fingered anything else. I see in the log file (/var/adm/cron/log) that an attempt is made to execute the command. I was told some of the characters on the line in the log could point to an answer. But, I am unable to decrpyt anything.

    I have the entry set to run a script I built, which runs when I manually type it. Also, I have another entry in the crontab file - that works - to run another script I built. Each script is set to run at a different time, so I don't think there would be any conflicts. I am not sure why cron will not complete it.

    Any advice is appreciated!

    ...now you can laugh
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4301
    It's hard to pinpoint the problem without seeing the log. However, one of the things to bear in mind is that the cron jobs run under a different environment than your regular login. In particular, cron doesn't have the same PATH variable as your login, hence you should specify all pathnames as absolute path names, to be safe. This is the #1 cause of errors in cronjobs.

    For example, if you wanted to print a list of files, you could simply type "ls" and get a list. This is because your PATH variable has "/bin" in it and so the shell can find it. With a cron job, you may need to type "/bin/ls" so that it can find the program.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Overseas - Azores
    Posts
    7
    Rep Power
    0
    Here is the line from crontab -l

    30 * * * * /d2/www/offline_prob/g_offline

    My script only has two commands and I am redirecting the output to another file. I'm still working with the script, so there will be more entries for it; however, I'd like to go ahead and use cron to automate it. BUT...problems...

    I didn't know about the PATH variable and cron...thanks for the info.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,198
    Rep Power
    17
    as scorpion said, cron has a very limited ENVIRON, not only PATH but all other
    variables set by the login-session are missed.
    q1: witch shell are you using
    q2: witch shell is cron using
    you have different way to fix that:
    - scorpion's suggestion: use full qualified names NOT only for cron but also
    for the script running
    penalty: you loose flexibility in the script.
    - set the envs using 'env'
    eg: * * * * * /bin/env XXX=xxx YYY=yyy /your-command
    now cron (/your-command) knows the values of $XXX and $YYY
    penalty: for each changes you have to reedit cronttab
    - FORCE cron to process your $HOME/dotfiles
    eg: * * * * * . $HOME/.profile ; /your-command
    note the dot before dollar
    - FORCE the script to process your $HOME/dotfiles (my preferred way)
    eg: * * * * * /your-command (assumed you use /bin/sh)
    and in /your-command is:
    #!/bin/sh
    . $HOME/.profile # again, note the dot at begin
    ..... the rest of code
    no penalty, the code will run

    ps: the only values known by cron are the entries in /etc/passwd for that user
    and (like scorpion said) a minimal PATH, nevertheless i expect '/bin' in it.

IMN logo majestic logo threadwatch logo seochat tools logo