crontab shell script wont run pls help

cttsite

Verified User
Joined
Jan 16, 2005
Messages
22
my shell script sql.sh
i can run with root SSH ./sql.sh
#!/bin/sh
###########################
service directadmin stop
service httpd stop
service mysqld stop
myisamchk -r -o /var/lib/mysql/*/*.MYI
service mysqld start
service httpd start
service directadmin start


but i cannot run by crontab -e
30 5 * * * sh /home/admin/sql.sh
30 5 * * * root /home/admin/sql.sh
30 5 * * * cd /home/admin/ && ./sql.sh
 
cron jobs don't have much in the way of environment variables, so things like $PATH are very sparse. Try using the full path to each executable, or setting and exporting the PATH in the beginning of the script. LD_LIBRARY_PATH might also need to be tweaked.
 
toml said:
cron jobs don't have much in the way of environment variables, so things like $PATH are very sparse. Try using the full path to each executable, or setting and exporting the PATH in the beginning of the script. LD_LIBRARY_PATH might also need to be tweaked.
thanks, can work now
 
Need Syntax

Hello sir!
i am new to shell programming. can you provide the full syntax for setting the path, bcoz i am getting the same problem too.
waiting for your kind reply......
 
Hello sir!
i am new to shell programming. can you provide the full syntax for setting the path, bcoz i am getting the same problem too.
waiting for your kind reply......

That depends on the shell you use
export PATH=$PATH:/path/to/some/bin/directory (bash,ksh)
PATH=$PATH:/path/to/some/bin/directory;export PATH (sh - borne shell not bash, but this works on bash and ksh too)
setenv PATH $PATH:/path/to/some/bin/directory (csh,zcsh)

I am sure I missed a few, but you should get the idea. Most open source OS will default to bash, so the first or second formats should do it.
 
The only reason the top shell script wouldnt work is because /sbin wasnt in the $PATH.

You can usually see this if you do:

su instead of su -

You cant execute things in /sbin because its not in your $PATH.

You either would have to set PATH in your shell script or call commands like:

servicecmd=$(whereis service)

$servicecmd httpd stop

The whereis command will grab the full path of the command if it can find it :D

Or else you have to just put the full path to each command in the file.

But setting $PATH is probably the easiest way if the command you are trying to execute is outside of your PATH.
 
@fahedprince:

You should never depend on a path existing for a cronjob; you should use a complete path for every call to a file, directory, or program.

Jeff
 
Back
Top