Operating System - Linux
1755504 Members
3481 Online
108834 Solutions
New Discussion юеВ

crontab job and run on shell

 
SOLVED
Go to solution
hangyu
Regular Advisor

crontab job and run on shell

I hv a crontab job (script) that can be run normally , now I try to run it on the shell but can't be run ( I sure I don't have any typing mistake as I copy the statement from the crontab ) , can advise what is difference between run a script on crontab and manually ? thx
6 REPLIES 6
Stuart Browne
Honored Contributor
Solution

Re: crontab job and run on shell

Environment.

The cron environment has nowhere near the PATH and other shell environment available to it.

So if you rely upon a shell-based environment variable or 'alias', these need to be put into the shell script you're running from cron.

Also note, that you *MUST HAVE* the hash-bang ('#!/bin/bash') at the start (along with executable permissions), otherwise cron won't know how it executes.
One long-haired git at your service...
hangyu
Regular Advisor

Re: crontab job and run on shell

thx reply,

I still have a bit misunderstand , do you mean I need to add ('#!/bin/bash') at the beginning of my script ? if not that , can advise what I need to do ? thx
Ivan Ferreira
Honored Contributor

Re: crontab job and run on shell

What is the error that you get? Ensure that a line was not divided in two during the copy/paste operation.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Wilfred Chau_1
Respected Contributor

Re: crontab job and run on shell

agree. environment is the issue.
also please add the path of the command to the PATH environment variable. Like PATH=$PATH:/usr/lcoal/bin

or use full path for all the commands in the script.
Ivan Ferreira
Honored Contributor

Re: crontab job and run on shell

Remember that the problem is not as usual. The problem is that the script works in cron but does not work in the shell.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
James R. Ferguson
Acclaimed Contributor

Re: crontab job and run on shell

Hi:

As noted, the environment in which a 'crontask' runs is sparse. Instead of adding the environmental variables you commonly need (e.g. PATH, etc.) to every script that you 'cron', source (read) a file that you define specifically to hold environmental variables.

Thus, your scripts would begin something like this:

# cat ./myscript
#!/bin/bash
. /home/hangyu/config
echo "hello ${WHO}"
exit 0

# cat ./config
WHO=Hangyu

Then run:

# ./myscript

...Now you have defined *one* configuation file that you can modify for *all* your scripts' needs.

Regards!

...JRF...