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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple linux script

Status
Not open for further replies.

dustervoice

Technical User
Apr 2, 2009
56
0
0
GB
Im having an issue getting a script to run in my linux box. this is what i have done so far.

Ive created a simple script. echo "hello world" save it as hello ,chmod 755 . when i run the ./hello the script runs fine. so i want this script to run every one minute on the box so this is my entry in my crontab file...

01 * * * * root /home/myscripts/hello

for some reason this isnt working im log in as root. any ideas? im new to scripting.
 
Im also getting an email to root saying "environment variable not set"
 
Does your script have a shebang as the first line?
Code:
#!/bin/bash
If not, when cron runs it probably can't figure out the interpreter to run it with.
 
Yes it does. i also ran it ./hello and it works i also change

01 * * * * root /home/myscripts/hello to
* * * * * root /home/myscripts/hello


still no luck
 
Why do you have the word "root" in front of your command? What does that command do?

Try changing it to...
Code:
* * * * * /home/myscripts/hello > hello.log 2>&1
That will put the output into a file called "[tt]hello.log[/tt]".


 
If you don't redirect the output of the script to a file (like I showed), the output, including error messages, will be mailed to your account. Try looking in your email to see if you have errors from the previous runs.

 
SamBones, quite correct, though I would for clarity change the CRONTAB entry to:
Code:
* * * * * /home/myscripts/hello 1>hello.log 2>&1

and for clarity for the OP, 1 = STDOUT and 2 = STDERR, and the above will redirect STDERR to STDOUT (2>&1) which is redirected to hello.log (1>hello.log)...

Ben
"If it works don't fix it! If it doesn't use a sledgehammer..."
How to ask a question, when posting them to a professional forum.
Only ask questions with yes/no answers if you want "yes" or "no"
 

dustervoice,

Can you determine which environment variable is being referred to as not set?

 
SamBones said:
Why do you have the word "root" in front of your command? What does that command do?

That's the syntax you would use in the "global" crontab file, /etc/crontab to tell it which user to run the job as.

Annihilannic.
 
* * * * * /home/myscripts/hello > hello.log 2>&1



tried that no log file is generated i even ran it as root still nothing.
 
i even wrote another one in the cron tab as ***** root echo ls

and is send it to an email... all the scripts are running but its going to root email not printing on the screen...
 
have you tried this?

01 * * * * root /home/myscripts/./hello
 
How are you adding this crontab entry? Since your example entry had "root" in front of the command, I'm assuming it's the global crontab, [tt]/etc/crontab[/tt], as Anni mentioned.

According to the man pages, this file must NOT be writable by any other user than root, no crontab files may be links, or linked to by any other file, and no crontab files may be executable, or be writable by any user other than their owner.

Does your [tt]/etc/crontab[/tt] meet these requirements? Is your "hello" script owned by root?

Also, since you are getting an email complaining about an environment variable, I think we can assume the script is getting run by cron. Try changing your hello script to this...
Code:
#!/bin/bash

/bin/date

/bin/echo "Hello, World!"
There's nothing there that requires an environment variable (all paths specified). Also, the "date" command will give you a timestamp, so you know it's a new/current message. Make sure it's owned by root, since you are trying to run it as root...
Code:
chown root:root /home/myscripts/hello
chmod 744 /home/myscripts/hello
Then, put it into [tt]/etc/crontab[/tt] with the following entry...
Code:
* * * * * root /home/myscripts/hello >> /home/myscripts/hello.log 2>&1
Then look for [tt]/home/myscripts/hello.log[/tt] after a minute or so. It should append to it every time it runs.

If it's still not working, could you please post your full "hello" script, and your full [tt]/etc/crontab[/tt]?


 
Ive used your example below and it writes it to the log file correctly every minute... which is great but i wanted it to print to the console! shouldnt /bin/echo "hello world" print to the console if i remove the redirection to a log file? i tried that but it didnt work.

#!/bin/bash

/bin/date

/bin/echo "Hello, World!
 
No, like I said before, if you don't redirect the output, it will be mailed to the account it's running under.

As long as it's running as [tt]root[/tt], you can do this to get it on the console...
Code:
* * * * * root /home/myscripts/hello >> /dev/console 2>&1
Just keep in mind that EVERYTHING output by the script will dump onto the console. Even if you're logged on there doing something else.

If you want to just capture message in the system log, look at the command "[tt]logger[/tt]" in the [tt]man[/tt] pages. That will let anyone send messages to the system log [tt]/var/log/messages[/tt].


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top