1831348 Members
2960 Online
110024 Solutions
New Discussion

Re: activating crontab

 
SOLVED
Go to solution
Shivkumar
Super Advisor

activating crontab

Dear Sirs,

I just put a shell script in crontab of the root.

I used the command crontab -e.

Do i need to use any command to activate this ?

Thanks,
Shiv
15 REPLIES 15
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: activating crontab

No, when you save the file a crobtab command is automatically executed. However, the preferred way (meaning the way that I do it) is:

crontab -l > user_crontab
edit user_crontab using the editor of your choice
crontab < user_crontab # updates your existing crontab entry and sends a SIGHUP to cron.

This method allows you to actually keep a copy of your file. You can always use contab -l to list your current crontab entries.

As with all the variant of crontab, take care because if you simply entry "crontab"; the command will wait for you to enter data and then rewrite your (now probably empty) crontab.


If it ain't broke, I can fix that.
Manuales
Super Advisor

Re: activating crontab

if you put a programm there, in cront, you have to make the scripts and develop it with all abolutes paths.

<>< Manuales
Shivkumar
Super Advisor

Re: activating crontab

I have submitted the cron job. I checked with "ps -ef|grep scriptname" command but it is not showing up.
James R. Ferguson
Acclaimed Contributor

Re: activating crontab

Hi SHiv:

You will not see your command run until it's time specification has been met.

You can find the return code (rc) of your crontask in '/var/adm/cron/log'.

Remember that the environment that the 'cron' daemon establishes is very sparse. Most notably, your PATH is only :

PATH=/usr/bin:/usr/sbin:.

Thus, unless you source your $HOME/.profile or some other file of environmental variables that your script needs, they will be lacking. This is one of the most usual reasons for finding that a script runs OK until it is added to a crontab.

Regards!

...JRF...
Shivkumar
Super Advisor

Re: activating crontab

Here is entry for cron job:-

* * * * * /home/sksonkar/scripts/ROSE/PINK-RED.sh >/dev/null 2>&1

I have given first line in the script as #!/usr/bin/ksh.

------

The output of "env" is has PATH set as:-

PATH=/opt/socks/bin:/usr/bin:/usr/ccs/bin:/usr/contrib/bin:/opt/fc/bin:/opt/fcms/bin:/opt/nettladm/bin:/opt/upgrade/bin:/opt/pd/bin:/usr/bin/X11:/usr/contrib/bin/X11:/opt/pb/bin:/opt/resmon/bin:/usr/sbin/diag/contrib:/opt/pred/bin:/opt/perf/bin://opt/perl/bin:/opt/hparray/bin:/opt/sec_mgmt/bastille/bin:/opt/pwplus/bin:/opt/graphics/common/bin:/opt/OV/bin/OpC:/usr/sbin:.

The other line is:-
PWD=/home/sksonkar/scripts/ROSE

Please suggest how do i source the environnment variable "=/home/sksonkar/scripts/ROSE" permanently.

Regards,
Shiv
James R. Ferguson
Acclaimed Contributor

Re: activating crontab

Hi Shiv:

One very clean way to handle environmental variables that you may want to use generally in scripts, is to create your own file that you can "source" (read) whenever you need it. For example:

# cat /home/sksonkar/env
#!/usr/bin/sh
export PATH=$PATH:/home/sksonkar/scripts/ROSE
export ME=shivkumar

Then, when you want the your environement to contain your updated PATH and the varible ME (for example) you add this to the script that needs this information:

. /home/sksonkar/env

...Note the dot character (".") followed by a space (blank) followed the absolute path of the file you want to read "into" your script. This is what is called "sourcing" a file.

The concept is that you make ONE copy of key variables you need and then you read them whenever you need them. It's one-stop maintenance when you want to change something!

The file of your variables can also be sourced at the end of your login profile.

Regards!

...JRF...
Yogeeraj_1
Honored Contributor

Re: activating crontab

hi shiv,

I prefer to spool the error and output logs to a logfile so that i can verify it if anything went wrong.

e.g.
* * * * * echo "/home/yogeeraj/sql/scheduled/fetchdata.sh" | su - oracle 1>/home/yogeeraj/logfiles/output-fetchdata.crn 2>/home/yogeeraj/logfiles/error-fetchdata.crn
#*******************************************************************************
# min|hour |day |month|day |script
# | |of mo| |of wk|
#----|-----|-----|-----|-----|--------------------------------------------------
#*******************************************************************************
#*******************************************************************************
# END OF TABLE day0->Sunday day6->Saturday
#*******************************************************************************

in the case, you are running it every minute, this might not be too relevant though.. but might help troubleshoot.

regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Shivkumar
Super Advisor

Re: activating crontab

I exported the variable and it worked.
i used the command
export PATH=$PATH:/home/sksonkar/scripts/ROSE

I have the root access ( power broker ). This script is owned by some other user.

This script will be running in the crontab of "root" user.

When i exited the root shell; script stopped working. I think the environment variable have got lost.

How do I make it a permanent change ?

Regards,
Shiv
Patrick Wallek
Honored Contributor

Re: activating crontab

YOu really should add this line:

export PATH=$PATH:/home/sksonkar/scripts/ROSE

into the script you are running. That will set the path every time the script runs.
Shivkumar
Super Advisor

Re: activating crontab

Patrick,

I just added below in the script:
export PATH=$PATH:/home/sksonkar/scripts/ROSE

It didn't help.

Do i need to do sourcing etc too ?
A. Clay Stephenson
Acclaimed Contributor

Re: activating crontab

If this script, in turn, calls other shell scripts or executables that are outside cron's very limited PATH then yes. The other option is to make sure that all the scripts within your parent script use absolute paths --- and the ones that these scripts in turn call --- and so on. That's why is is better to source an environment file. THat will take care of PATH plus any other needed environment variables.
If it ain't broke, I can fix that.
Shivkumar
Super Advisor

Re: activating crontab

What are the steps required to source the path ?

I am using power broker root access.

Here is the path where the scripts are stored :
/home/bigman/scripts/ROSE/

The user is someone else.

I belive env variable are stored in user's .profile file.

The current script will be running as power broker (root) user because user is not permitted to run cron job with his own id.

Thanks,
Shiv



A. Clay Stephenson
Acclaimed Contributor

Re: activating crontab

Reading in the user's .profile is less than perfect because the .profile will almost certainly have statements (e.g. tset, tput, tabs) which will depend upon an interactive environment -- which you ain't under cron.

My preferred method is to actually yank out all of the required environment setup command out of the the user's .profile and put them in a file (e.g. /usr/local/bin/userfredstuff.sh). This file miust not contain an exit or return statement.

Now both your script and fred's .profile source this file via the . (dot) operator.
. /usr/local/bin/userfredstuff.sh.

This means that the environment variables only have to be changed in one place and in other user's need the same environment they can source this file as well.

The other method is to source .profile but surround all of the command which expect stdin to be a terminal with
if [[ -t 0 ]]
then
tset ...
tput ...
fi

If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: activating crontab

Hi Shiv:

You asked "what are the steps required to source the path".

If you return to my response above you will see that "sourcing" is a facny term for "reading". To source one file "into" another you use the notation of a dot ("." followed by a space character, followed by the path of the script that you want to be read. An absolute path is preferred.

You *can* source your $HOME/.profile although this leads to other issues unless you take care of them first in your profile! (but that's another subject).

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: activating crontab

Hi Shiv:

You asked "what are the steps required to source the path".

If you return to my response above you will see that "sourcing" is a facny term for "reading". To source one file "into" another you use the notation of a dot (".")followed by a space character, followed by the path of the script that you want to be read. An absolute path is preferred.

You *can* source your $HOME/.profile although this leads to other issues unless you take care of them first in your profile! (but that's another subject).

Regards!

...JRF...