Operating System - HP-UX
1826797 Members
3057 Online
109703 Solutions
New Discussion

Starting a process with cron

 
Jerry Timm
Occasional Contributor

Starting a process with cron

I have a process that needs to be started each night after my backup. It must be started as a user other than root. I can start it while logged in as this user, but it won't start from it's crontab file. I think that $Path is not set. How do I use cron and set the $PATH as if the user was logged on
5 REPLIES 5
Edward Alfert_2
Respected Contributor

Re: Starting a process with cron

is the user that needs to run the process the same user that runs the backup?

If so, why not just append the commands to the end of the backup script.

I do this with several processes that i run after my nightly backup.
"Do what you love and you will never work a day in your life." - Confucius
James R. Ferguson
Acclaimed Contributor

Re: Starting a process with cron

Hi Jerry:

The environment supplied by 'cron' is very sparse. You can source your $HOME/.profile ahead of calling your script. This can be done in the crontab entry. Alternatively, you can source your profile within the script itself thereby providing the necessary environmental variables (including PATH). Thirdly, you could create a new file which contains and exports the common variables needed for your application, and source (read) that file within your standard profile; within standard scripts; and/or in a crontab stream.

To source (read) a file within a script, put a dot (".") in front of the script name. For instance, to source a file called 'myscript' you would do:

# . ./myscript #...note the space between the dot and the script name.

While you can source your $HOME/.profile, this generally leads to the annoying "not a typewriter" messages when commands like 'stty' are executed in a non-interactive environment. You could redirect stdout and stderr to /dev/null although you may miss important messages this way, or you could enhance your profile to execute these commands like this:

if [ -t 0 ]
then
stty...
fi

This leads full-circle to the idea of creating a file of environmental variables you need in a file that can be sourced independently of the profile and/or by the profile during login processing.

Regards!

...JRF...
Sanjay_6
Honored Contributor

Re: Starting a process with cron

Hi Jerry,

You can set your cron job for that user like this.

run "crontab -e" as that user. It will open the file for cron jobs already set by that user else it will open a new file in vi mode. You edit this file and define the schedule and comamnd to be run by cron in this file. The format will be

min hrs day_month month day_week command

so if want to set up a job to be run at say 11pm every night and the job file is /users/user1/cronjob.sh you have to put these values

00 23 * * * /users/user1/cronjob.sh 2>1&

save and exit. You don't have to specify the name of the file where you are saving. Just :wq!

The job is now set into cron. You can look at it by using
"crontab -l"

Read the man pages of crontab for more details.

Hope this helps

Thanks
Magdi KAMAL
Respected Contributor

Re: Starting a process with cron

Hi Jerry,

The following command in the root crontab file will do what you need :

10 22 * * * /bin/su - UserName -c "/users/user1/bin/user1Script.sh arg1 arg2"

So, the script user1Script.sh will be running under the environment of user1.

Magdi
Magdi KAMAL
Respected Contributor

Re: Starting a process with cron


Oops, "UserName" stand for user1 in this case.

Magdi