nohup in cron job

Hello,
I have bunch of shell scripts, which I want to execute every hour in the background. So I created a script mainscript.sh which executes these hourly scripts in the background. Script goes like this.
mainscript.sh

#!/bin/sh
nohup sh subscript1.sh &
nohup sh subscrip2.sh &
exit 0

And I set up this mainscript.sh script through cron to execute every hour. When I run this script manually it runs all other scripts properly in the background. But nothing is happening with cron setup.

Your help is appreciable

thanks
papachi_2000

What does your cron entry look like?

Why are you using nohup? Nohup is for if you are logged in, and don't want the job to exit when you log out, if you start the job from a terminal.

Cron has no terminal associated with it. Therefore, nohup does not help you.
-Mike

1 Like

Hi thr,
try to put ". .profile" at the start of your calling script.

I guess "&" does not work in cron.

Regards
Abhijeet

I used nohup because like mike mentioned I don't want the job to exit when I log out. If Cron has no terminal associated with it, So that means I don't have to use nohup?
But I want all shell scripts to execute background. If I don't use '&' would they run in back ground? I don't want them to wait for other shell to execute. So to achieve my requirement what should I do? And abhijeet I did used '. .profile' still it won't work.

Thanks to all,
Papachi

You don't need the nohup, because the script is running without being linked to a terminal session and nohup is needed when you want to end a session and keep a job running.

I could be wrong, but I think you're right to use the Ampersand to allow both subscripts to run concurrently.

I think the point Abhijeetkul was making about the .profile was that the environment used by the Cron job (root's?) will be different from your own and it may be that the PATH variable is not the same, so the job as run by the cron may not be able to find your script, or subscripts. You could try logging in as root (or whatever user's cron you're using) and running the script using the same command as entered in the cron to see if it works.

cheers

Hello all,
I faced a similar problem recently.Only exception was i need not have "&".
All my scripts were working fine when run manually,but not the same in cron.

I thought of calling my user's .profile in it & i succeeded.

Hope your problem solves out soon.

Try . $HOME/.profile

Regards
Abhijeet