cancel
Showing results for 
Search instead for 
Did you mean: 

how to configure cron

SOLVED
Go to solution
rustam_2
Super Advisor

how to configure cron

Hi all,
I need to configure cron task for running several patches by turn.
I read documentation and now need some practical advices. Planning these steps:
1. I checked, does this service run or doesnt
$ps -ef |grep cron
root 1633 1 0 Oct 29 ? 0:18 /usr/sbin/cron
my_user 5535 20242 0 11:28:56 console 0:00 grep cron
so here is ok.
2. I checked, does my_user exist in cron.allow tab.
$cd cd /var/adm/cron
$less cron.allow
root
adm
uucp
so my_user doesnt exit here, should i add my user? is it ok if i add with vi?
3. My file rename.sh is in /u02/scripts/ and i manually run this script like this $./rename.sh

So how can i add command in crontab file using crontab -e? should i show my command so: /u02/scripts/./rename.sh? or it's wrong syntax? it is a bit complicated. So help me, please.

Regards,
Rustam
53 REPLIES
Jose Mosquera
Honored Contributor
Solution

Re: how to configure cron

Hi,

Yes, you must add your user "my_user" with vi editor. if you not be skilled with vi editor I suggest you make first a copy of cron.allow file.
#cp -p cron.allow cron.allow.bak
You also add an entry in your cron.allow with this:
#echo my_user >> cron.allow
For "crontab -e" command you must indicate de full path and filename, the file must be enough privileges for execution, in your case "/u02/scripts/rename.sh" is fine. The correct syntax of crontab command can be viewed:
#man crontab

Rgds.
Hakki Aydin Ucar
Honored Contributor

Re: how to configure cron

The most important thing with crontab, you must be sure all commands in your script must have full path due to cron does not use your shell.
Jose Mosquera
Honored Contributor

Re: how to configure cron

Hi again,

Right! which indicates Hakki is important, the cron does not load the definition of environment that normally a user loads when login to the system (/etc/profile, /etc/PATH, homedir user's profile, etc). Other way could be set and export PATH variable as Global enviroment definition, and any others specific variables requested as particular environment definitions (ORACLE_HOME, ORACLE_SID, etc). With these definitions you have search/execute capabilities for commands.

Rgds.
rustam_2
Super Advisor

Re: how to configure cron

Thanks Jose and Hakki,

I did following actions:
1.Add to cron.allow file my_user
#echo my_user >> cron.allow
2.Opened crontab file with command
$crontab â e and add this line
50 14 * * * /u02/scripts/rename.sh
For running my script at 14:50 everyday
3.$crontab â l shows me
50 14 * * * /u02/scripts/rename.sh

But my script did not run. I checked log file /var/adm/cron/log and found nothing according my script.
What is problem? Does not my user have some privileges to /var/spool/cron/crontabs/my_user file? Or as Hakki said my script has problem? If script has problem log file must have error information. I guess cron doesnt know something about my job.
My script includes this line
mv my_file.log my_file_$(date +%m%d%Y).log

Regards,
rustam
Jose Mosquera
Honored Contributor

Re: how to configure cron

Hi,

Have you tested your script at the prompt before try with the cron?
Have your script enough execution privilegues, please post us this command output:
#ls -l /u02/scripts/rename.sh

Rgds.
rustam_2
Super Advisor

Re: how to configure cron

Yeah Jose, i tested manually several times. It works perfect and does what i what. All users have privileges:
$ ls -l /u02/scripts/rename.sh
-rwxrwxrwx 1 root sys 40 Feb 21 15:34 /u02/scripts/rename.sh
Jose Mosquera
Honored Contributor

Re: how to configure cron

Could we see the contents of the script?
rustam_2
Super Advisor

Re: how to configure cron

Jose, yeah, you could.

>>Could we see the contents of the script?

I gave at 12:40:42 GMT

>>My script includes this line
>>mv my_file.log my_file_$(date +%m%d%Y).log
Jose Mosquera
Honored Contributor

Re: how to configure cron

Hi,

Ooops, is too simply!!! :)

Ok, I guess that directory where run this have enough privileges (execution/write).

Well, this looks a search/execution command path issue. As we have indicate you should include the full path for used commands, or set PATH variable to locate them.
A way to know the full path of a command is:
#whence date
A full path assigned in Global PATH variable will be reported.
Then replace:
date +%m%d%Y
By:
/fullpath/date +%m%d%Y

Please try this...
rustam_2
Super Advisor

Re: how to configure cron

Hi Jose,

I changed my script value
$vi rename.sh
mv my_file.log my_file_$(/usr/bin/date +%m%d%Y).log
checked script by running manually and it renamed my file. Then changed running time of cron task
$crontab -e
50 22 * * * /u02/script/rename.sh
it had to run my script at 22:50 but didnt :(

I noticed 1 line messages in log file about rename file:
> CMD: /u02/dump/rename.sh

there are some information which generates every 15 minutes during whole day last half year. dont know what is it. Dont you know what is it? Maybe this makes some barer?

> CMD: /opt/sfm/bin/restart_sfm.sh
> root 12270 c Mon Feb 21 22:05:00 WST 2011
> CMD: /etc/opt/resmon/lbin/mon_EMSHAProvider_state.sh
> root 12271 c Mon Feb 21 22:05:00 WST 2011
< root 12270 c Mon Feb 21 22:05:01 WST 2011
< root 12271 c Mon Feb 21 22:05:01 WST 2011
> CMD: /opt/sfm/bin/restart_sfm.sh
rustam_2
Super Advisor

Re: how to configure cron

Hi Jose,

I changed my script value
$vi rename.sh
mv my_file.log my_file_$(/usr/bin/date +%m%d%Y).log
checked script by running manually and it renamed my file. Then changed running time of cron task
$crontab -e
50 22 * * * /u02/script/rename.sh
it had to run my script at 22:50 but didnt :(

I noticed 1 line messages in log file about rename file:
> CMD: /u02/dump/rename.sh

there are some information which generates every 15 minutes during whole day last half year. dont know what is it. Dont you know what is it? Maybe this makes some barer?

> CMD: /opt/sfm/bin/restart_sfm.sh
> root 12270 c Mon Feb 21 22:05:00 WST 2011
> CMD: /etc/opt/resmon/lbin/mon_EMSHAProvider_state.sh
> root 12271 c Mon Feb 21 22:05:00 WST 2011
< root 12270 c Mon Feb 21 22:05:01 WST 2011
< root 12271 c Mon Feb 21 22:05:01 WST 2011
> CMD: /opt/sfm/bin/restart_sfm.sh

regards,
rustam
Dennis Handly
Acclaimed Contributor

Re: how to configure cron

>2. Opened crontab file with command

(On Unix systems you edit/view files, not open.)

>But my script did not run.

Add "set -x" to your script to trace execution.

>If script has problem logfile must have error information.

No, the stdout/stderr gets mailed to that user.

>My script includes this line
mv my_file.log my_file_$(date +%m%d%Y).log

That should work fine. You may have problems trying to do this more than once a day.

Also your script should start with:
#!/usr/bin/sh

>All users have privileges:
$ ls -l /u02/scripts/rename.sh
-rwxrwxrwx root sys /u02/scripts/rename.sh

This is a security hole. You shouldn't allow write access:
chmod go-w /u02/scripts/rename.sh

>Do you know what is it?
/opt/sfm/bin/restart_sfm.sh

These are part of System Fault Management. Leave it alone.

>Jose: As we have indicate you should include the full path for used commands

In this case mv and date are in the the default PATH.
Jose Mosquera
Honored Contributor

Re: how to configure cron

Hi Rustam,

Please ensure that you are inside of a "my_user" session, and at the system prompt executes the command with this trace option: sh -x /u02/script/rename.sh
What happened?

Now try to generate a cumulative trace file at the "crontab -e" session, then replace your current entry by:
50 22 * * * sh -x /u02/script/rename.sh|tee -a /tmp/rename.log
After run it please check te content of /tmp/rename.log file.

Rgds.
Jose Mosquera
Honored Contributor

Re: how to configure cron

Hi again,

Please ensure that actions above indicated always been done from a "my_user" session.

Rgds.
Dennis Handly
Acclaimed Contributor

Re: how to configure cron

>Jose: Now try to generate a cumulative trace file:
50 22 * * * sh -x /u02/script/rename.sh | tee -a /tmp/rename.log

No need to use tee. And you need to capture stderr:
50 22 * * * sh -x /u02/script/rename.sh >> /tmp/rename.log 2>&1
rustam_2
Super Advisor

Re: how to configure cron

Hi Dennis and Jose,

I add trace in crontab file
crontab -l
11 11 * * * sh -x/u02/scripts/rename.sh >> /tmp/rename.log 2>&1

script has this line:
mv my_file.log my_file_$(/usr/bin/date +%m%d%Y).log

I got rename.log file in /tmp directory.

The file includes this message:
-x/u02/scripts/rename.sh: A specified flag is not valid for this command.

So what is wrong? I guess something wrong in my rename.sh.

Regards,
rustam
Hakki Aydin Ucar
Honored Contributor

Re: how to configure cron

>mv my_file.log my_file_$(date +%m%d%Y).log

did you try it this way ;

/usr/bin/mv my_file.log my_file_$(/usr/bin/date +%m%d%Y).log
rustam_2
Super Advisor

Re: how to configure cron

Hi Hakki,

>>did you try it this way ;
>>/usr/bin/mv my_file.log my_file_$(/usr/bin/>>date +%m%d%Y).log

I tried and still have message in my /tmp/rename.log file

-x/u02/scripts/rename.sh: A specified flag is not valid for this command.

All syntaxes work correctly manually.
Hakki Aydin Ucar
Honored Contributor

Re: how to configure cron

try 1:
11 11 * * * /usr/bin/sh -x/u02/scripts/rename.sh >> /tmp/rename.log 2>&1

try 2:
chmod 755 /u02/scripts/rename.sh
11 11 * * * /u02/scripts/rename.sh >> /tmp/rename.log 2>&1
rustam_2
Super Advisor

Re: how to configure cron

Hakki,

>>try 1:
>>11 11 * * * /usr/bin/sh -x/u02/scripts/>>rename.sh >> /tmp/rename.log 2>&1

it gave nothing new. In log file:
-x/u02/scripts/rename.sh: A specified flag is not valid for this command.

>>try 2:
>>chmod 755 /u02/scripts/rename.sh
>>11 11 * * * /u02/scripts/rename.sh >> /tmp/>>rename.log 2>&1

file didnt rename but in log file new event. Event about privileges
mv: my_file.log: cannot access: No such file or directory

crontab -l
15 15 * * * /u02/scripts/rename.sh >> /tmp/rename.log 2>&1

less rename.sh
/usr/bin/mv my_file.log my_file_$(/usr/bin/date +%m%d%Y).log
V. Nyga
Honored Contributor

Re: how to configure cron

Hi,

I don't know if I had the same problem, but I've splited it, so you can try this:

#!/usr/bin/sh

DATUM='date +%m%d%Y'
mv my_file.log my_file_$DATUM.log


HTH
V.
*** Say 'Thanks' with Kudos ***
Jose Mosquera
Honored Contributor

Re: how to configure cron

Hi Rustam,

Happens because you have a space omission among "sh -x" and /u02/scripts/rename.sh. Please ensure this one into "crontab -e" session:
11 11 * * * sh -x /u02/scripts/rename.sh >> /tmp/rename.log 2>&1

Would you matter changing the syntax of your script with this one? (the result is the same):
#!/usr/bin/sh
TIMESTAMP=`date +%m%d%Y`
WORKDIR=Please just indicates the full path where the "my_file.log" is placed.
cd $WORKDIR
mv my_file.log my_file_${TIMESTAMP}.log


Rgds.

Hakki Aydin Ucar
Honored Contributor

Re: how to configure cron

one second though;
>/usr/bin/mv my_file.log my_file_$(/usr/bin/date +%m%d%Y).log

are you sure in yur file is on correct place ?
/usr/bin/mv //my_file.log my_file_$(/usr/bin/date +%m%d%Y).log
rustam_2
Super Advisor

Re: how to configure cron

I tested various methods.
Now i have:
$crontab -l
32_17_*_*_*_/usr/bin/sh_-x/u02/scripts/rename.sh_>>_/tmp/rename.log 2>&1
_ means space between words

less rename.sh
/usr/bin/mv /u02/scripts/my_file.log /u03/export/my_file_$(/usr/bin/date +%m%d%Y).log

and checked this command line also:
/usr/bin/mv /u02/scripts/my_file.log my_file_$(/usr/bin/date +%m%d%Y).log

Still all various commands in rename.sh work correct manually.