Koozali.org: home of the SME Server

Add job to Crontab

Offline loejf

  • ****
  • 74
  • +0/-0
Add job to Crontab
« on: June 08, 2009, 10:32:16 AM »
Hi.
I have made at script /root/script/myscript.sh
with chmod 755
And in crontab I have added:
00,30 * * * * root /root/scripts/myscript.sh
and /var/log/ shows
Jun  8 10:30:01 ydun crond[4817]: (root) CMD (/root/scripts/myscript.sh)

But the problem is, the scripts does not do the work, but if I run it manually, it does.
Any suggestions?
Loejf
...

Offline mmccarn

  • *
  • 2,656
  • +10/-0
Re: Add job to Crontab
« Reply #1 on: June 08, 2009, 04:19:47 PM »
You might try using complete specifications for all executables in your script - that is, use /bin/program or /usr/bin/program instead of simply running program and trusting PATH to be correct.

Figure out a way to make your program more informative, then you'll get more info in the log files...

Make sure (with permissions of 755) that the script is owned by the user "root"

Offline loejf

  • ****
  • 74
  • +0/-0
Re: Add job to Crontab
« Reply #2 on: June 08, 2009, 04:36:39 PM »
Hi.
Right now the trial script only runs an echo, and pipe this into a logfile.
And when I run it manually, it works.
The script is owned by root, and has permissions 755, including the folder.
Could it be something like, when cron runs, is does not use the root environment, but its own ??
Loejf.
...

Offline loejf

  • ****
  • 74
  • +0/-0
Re: Add job to Crontab
« Reply #3 on: June 08, 2009, 05:18:45 PM »
Hi. I have tried to follow this way to do it:

mkdir /etc/e-smith/templates-custom/etc/crontab/

vim  /etc/e-smith/templates-custom/etc/crontab/fileprocessor.cron
#This text was then added to the file:
#FILEPROCESSOR SCRIPT
00,30 * * * * root /root/FILEPROCESSOR/fileprocessor.sh

/sbin/e-smith/expand-template etc/crontab

more /etc/crontab - just to verify your new entry is there, then

service crond restart
...

Offline loejf

  • ****
  • 74
  • +0/-0
Re: Add job to Crontab
« Reply #4 on: June 08, 2009, 05:40:45 PM »
Hmmmm.
It doesn´t make any difference.
Same result.
...

Offline Stefano

  • *
  • 10,894
  • +3/-0
Re: Add job to Crontab
« Reply #5 on: June 08, 2009, 07:18:37 PM »
loejf, as mmccarn told you, you'd use full path to executable files in your script.

try (example, adapt to your needs)
Code: [Select]
...
MV='which mv'
...
$MV oldfile newfile

hth
ciao
Stefano

Offline loejf

  • ****
  • 74
  • +0/-0
Re: Add job to Crontab
« Reply #6 on: June 08, 2009, 07:55:49 PM »
Hi.
Yes, but I do that.
The only executable file is "fileprocessor.sh", and it is located "/root/FILEPROCESSOR/fileprocessor.sh", and that is what I have added to Cron.
Do I misunderstand something ?
Loejf.
...

Offline Stefano

  • *
  • 10,894
  • +3/-0
Re: Add job to Crontab
« Reply #7 on: June 08, 2009, 07:57:47 PM »
Do I misunderstand something ?

yes :-)

we are talking about instrunctions into fileprocessor.sh :-)

Ciao
Stefano

Offline cactus

  • *
  • 4,880
  • +3/-0
    • http://www.snetram.nl
Re: Add job to Crontab
« Reply #8 on: June 08, 2009, 08:24:40 PM »
Do I misunderstand something ?
Yes, since you only use echo (you stated earlier) make sure you do something like this:

Code: [Select]
#!/bin/sh
/bin/echo "Hello world!"

instead of

Code: [Select]
#!/bin/sh
echo "Hello world!"

To find the full path to a executable (within your root path on the CLI) you can use the 'which' program, e. g.:

Code: [Select]
which echo
should give you something like this:

Code: [Select]
[root@smetest ~]# which echo
/bin/echo
[root@smetest ~]#

There are ways to define the path inside scripts as well using:

Code: [Select]
PATH=/usr/bin:/usr/sbin:.
See for instance: http://blog.spikesource.com/crontab.htm
Be careful whose advice you buy, but be patient with those who supply it. Advice is a form of nostalgia, dispensing it is a way of fishing the past from the disposal, wiping it off, painting over the ugly parts and recycling it for more than its worth ~ Baz Luhrmann - Everybody's Free (To Wear Sunscreen)

Offline loejf

  • ****
  • 74
  • +0/-0
Re: Add job to Crontab
« Reply #9 on: June 08, 2009, 10:33:22 PM »
Hi.
I did add the following to the script:
USER=root
HOME=/root
LOGNAME=root
PATH=/sbin/e-smith:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
SHELL=/bin/bash

And it did it. So now it works.
Thank you very much for your help.

But it seems unnecessary difficult to get working ??
Loejf.
...

Offline cactus

  • *
  • 4,880
  • +3/-0
    • http://www.snetram.nl
Re: Add job to Crontab
« Reply #10 on: June 08, 2009, 10:59:46 PM »
But it seems unnecessary difficult to get working ??
That might be to you, you could also have just replaced echo by /bin/echo like also suggested, you did a little bit more than needed.

Keep in mind that cron jobs run in a chroot environment with minimal settings for security purposes as well, therefore amongst some other things a path is not set. I would advice you to not set a path and use the full filename for programs you invoke from your script or reduce the path environment variable to it's bare minimum as it is a little (too) extended at the least if I look at it.
Be careful whose advice you buy, but be patient with those who supply it. Advice is a form of nostalgia, dispensing it is a way of fishing the past from the disposal, wiping it off, painting over the ugly parts and recycling it for more than its worth ~ Baz Luhrmann - Everybody's Free (To Wear Sunscreen)