Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To run shell script in cronjob 2

Status
Not open for further replies.

lane78

Programmer
Oct 10, 2001
9
MY
Hi,

I want to run shell scripts in cronjob and i need to verify whether this syntax is correct.
The shell script files are send, usr and filels (all stored in the same folder servlet)

0 9 * * * /home/admin/report/servlet>send usr filels

I've typed the full path '/home/../servlet' to the scripts however, I think there are some mistakes here..Pls help with the correct syntax. FYI, the above command is working when directly executed at UNIX prompt.

/home/admin/report/servlet>send usr filels
Result:
mail sent!

Thanks
lane

 
What you want ( presuming these are three seperate scripts ) is ;
0 9 * * * /home/admin/report/servlet/send
0 9 * * * /home/admin/report/servlet/usr
0 9 * * * /home/admin/report/servlet/filels
Does this answer your question? SOL
"If I'm talking c**p, I'm probably p****d"
 
Hi,
I would want the three scripts to be executed together, coz 'send' needs 'usr' for the 'user' list, and 'filels' contains the list of filenames to be sent to the users. 'send' is an emailing script, fyi.

Thanks
lane
 
Could they be combined into one script? SOL
"If I'm talking c**p, I'm probably p****d"
 
Alternatively you could do
0 9 * * * /home/admin/report/servlet/send;/home/admin/report/servlet/usr;/home/admin/report/servlet/filels

All on the same line SOL
"If I'm talking c**p, I'm probably p****d"
 
*Way* too much work. :)

Create a small shell script, called svlt_wrap, in /usr/local/bin containing these four lines.

#!/bin/sh
/home/admin/report/servlet/send
/home/admin/report/servlet/usr
/home/admin/report/servlet/filels

then make it executable

chmod +x /usr/local/bin/svlt_wrap

and then a simple crontab entry like this

0 9 * * * /usr/local/bin/svlt_wrap Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Thanks SOL, Mike..

Actually i already have a shell script- mailfile containing 1 line.
./send usr filels
but my main problem now is that all these scripts get the same results: Execute permission denied. (I've chmod ugo+x for mailrpt, send, usr and filels)

I created the crontab file using crontab -e, and I notice it is a tmp file that keeps changing name everytime i type 'crontab -e' at prompt with -rw- - - - - - -. Above all, my cronjob task never works. Pls help..

p/s:Is there any way i can make the crontab file fixed with a specific filename?Actually where should this crontab file being stored?

Lane
 
OKay first, who's crontab file are you modifying? The root user or one of your own. If it is one of your own you may need to add your name to the cron.allow file, for HP_UX (my flavour ) this is in /var/adm/cron, it may be different for yours, the location is listed at the bottom of the crontab man page. I'm not sure I understand how you are trying to run these three programs. By using them all on the same line it kind of implies you are running the script "send" and passing it the files "usr" and "filesls", is this what you intend or should the files all run in their own right sequentially? SOL
The best thing about banging your head against a wall is when you stop.
 
Hi SOL,

'send' contains the emailing script, 'usr' provides the recipients names and 'filels' is the list of files to be sent to recipients.
At prompt, the mailfile works (./send usr filels) and emails are succesfully sent to the recipients.

../../servlet>mailfile
mail sent!

My userid is already in the cron.allow file, and i found that my crontab file is in var/tmp. (Should it be there?) I don't have specific name for the crontab file and it is a tmp file.

Lane

 
It may depend on the flavour of Unix you are using, but my crontab files are in /var/spool/cron/crontabs. I'd check here for crontab files.

As far as the cron line is concerned, I think I understand that you are passing two files into the first script and using the data as parameters $1 and $2 within the script. Your cron line should be
0 9 * * * /home/admin/report/servlet/send usr filels



SOL
The best thing about banging your head against a wall is when you stop.
 
Hi SOL,
Yes, done that (../../servlet/send usr filels) - still : Execute permission denied.
And yes, i can find my crontab file in /var/spool/cron/crontabs, named after my userid. Any idea why my cronjob task doesn't work?

**Not just this, i can't run 'java' command from here as well..(of course works fine at prompt)

lane
 
Okay, the script works from the command line so the permissions must be fine on these files. Okay I've checked if I call crontab -e, I also get a /var/tmp/<filename>, I'd never noticed this before. So that part is okay, it's just giving the file a temporary name before it writes to the crontab file.

I think if you try the following it should work.

Amend your script

/home/admin/report/servlet/mailfile

to contain the lines

cd /home/admin/report/servlet/
./send usr filels

Ensure it is executable and then add

0 9 * * * /home/admin/report/servlet/mailfile

to your crontab file.

If this doesn't work then send a copy of your crontab file, all scripts and a 'ls -l' of the /home/admin/report/servlet directory please.
SOL
The best thing about banging your head against a wall is when you stop.
 
Hi SOL,

Thanks for the help :)
I'll try to ammend mailfile..

lane

 
Hi, don't know whether this is the problem, but it could be that as cron doesn't inherit any environment variables (PATH) etc from the user's shell, it isn't able to carry out the required executes. What might be required is a line early in each script something along the lines of:

. /<user_home_dir>/.profile (note the space between . and /)

so that the user's .profile file is read before the script is run and environment variables set accordingly. I don't quite understand why the files are being regarded as non-executable, however. Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top