LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-23-2017, 01:40 PM   #1
krishnar
Member
 
Registered: May 2016
Posts: 32

Rep: Reputation: Disabled
Unhappy Learning Crontab


Hi All,

I have added my first cron job like this:

user@krishnar6:~$ crontab -l | grep -v "#"

* * * * * /home/user/test.sh

user@krishnar6:~$ cat /home/user/test.sh
#!/bin/bash
echo hello!
user@krishnar6:~$

I expected this job to run every minute and print hello output. But it never did. Can you please help me to fix the prob?

Krish
 
Old 05-23-2017, 01:44 PM   #2
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
The script will print to stdout, which cron sends to the configured email account.
Unless you have configured that in a sensible way you will never see it.

Try redirecting the output to a file:

Code:
#!/bin/bash
echo "hello!" > /tmp/crontest.out
Also make sure the script is executable:
Code:
chmod +x /home/user/test.sh
 
1 members found this post helpful.
Old 05-23-2017, 01:48 PM   #3
krishnar
Member
 
Registered: May 2016
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by joe_2000 View Post
The script will print to stdout, which cron sends to the configured email account.
Unless you have configured that in a sensible way you will never see it.

Try redirecting the output to a file:

Code:
#!/bin/bash
echo "hello!" > /tmp/crontest.out
Also make sure the script is executable:
Code:
chmod +x /home/user/test.sh
It worked!!
 
Old 05-23-2017, 02:03 PM   #4
krishnar
Member
 
Registered: May 2016
Posts: 32

Original Poster
Rep: Reputation: Disabled
I have modified my scrip like this:

user@krishnar6:~$ cat test.sh
#!/bin/bash
echo hello! > crontest.txt
mkdir /home/user/cron

The cron job creates the text file but NOT the folder. I am wondering why folder is not getting created.

Last edited by krishnar; 05-23-2017 at 02:08 PM.
 
Old 05-23-2017, 02:13 PM   #5
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Hmm, if all of this is happening as the same user this should actually work.

You can debug this by printing the output to a file. Change the crontab entry to

Code:
* * * * * /home/user/test.sh > /tmp/crontab.out 2>&1
The 2>&1 redirects stderr to stdout so that everything ends up in the out file.

Please post the content of /tmp/crontab.out after an (unsuccessfull) cron execution.
 
Old 05-23-2017, 02:19 PM   #6
krishnar
Member
 
Registered: May 2016
Posts: 32

Original Poster
Rep: Reputation: Disabled
user@krishnar6:~$ cat /tmp/crontab.out
/home/user/test.sh: line 3: mkdir: command not found
user@krishnar6:~$
 
Old 05-23-2017, 02:24 PM   #7
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by krishnar View Post
user@krishnar6:~$ cat /tmp/crontab.out
/home/user/test.sh: line 3: mkdir: command not found
user@krishnar6:~$
Ok, that explains. Cron has a reduced environment. That includes the content of the PATH variable. Therefore it does not find the mkdir executable.

Run
Code:
which mkdir
The output will probably be /bin/mkdir or /usr/bin/mkdir.

That is the full path to mkdir. Use that in the script. So e.g.:
Code:
/bin/mkdir /home/user/cron
For this reason it is good practice to always use absolute paths to commands in cronjobs.
 
1 members found this post helpful.
Old 05-23-2017, 02:32 PM   #8
krishnar
Member
 
Registered: May 2016
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thanks! That fixed the issue.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: What Is Crontab And How To Automate Tasks In Linux With Crontab LXer Syndicated Linux News 0 05-13-2017 05:45 PM
LXer: Machine Learning, Deep Learning 101 LXer Syndicated Linux News 0 07-20-2016 11:00 AM
Coding bootcamp learning platform and privacy issues with online learning. Need Advice ! slothbin Programming 2 04-01-2016 08:33 AM
[SOLVED] Problem with crontab:: command not executed properly via crontab Ankush Seth Linux - Newbie 11 11-11-2013 06:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration