LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-09-2019, 02:44 PM   #1
CoBE10
LQ Newbie
 
Registered: Jun 2019
Posts: 1

Rep: Reputation: Disabled
Problems with crontab


I have created cronjob which I want to execute script every 1 hour, but it doesn't work.

Here is the cronjob:

Code:
0 */1 * * * sh /home/sebi/.scripts/statusbar/speaktime.sh
And here is script speaktime.sh

Code:
#!/bin/bash
PATH=/bin/bash
printf "It's $(date +%R) o'clock" | espeak-ng
What have I done wrong?

Last edited by CoBE10; 06-09-2019 at 02:47 PM.
 
Old 06-09-2019, 03:05 PM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,498

Rep: Reputation: 4312Reputation: 4312Reputation: 4312Reputation: 4312Reputation: 4312Reputation: 4312Reputation: 4312Reputation: 4312Reputation: 4312Reputation: 4312Reputation: 4312
what do you mean by "it does not work"? Usually we have to use full path in crontab, so use /bin/bash instead of sh. shebang does not work if you explicitly specify the shell.
 
Old 06-09-2019, 03:12 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 22,217

Rep: Reputation: 5962Reputation: 5962Reputation: 5962Reputation: 5962Reputation: 5962Reputation: 5962Reputation: 5962Reputation: 5962Reputation: 5962Reputation: 5962Reputation: 5962
Quote:
Originally Posted by CoBE10 View Post
I have created cronjob which I want to execute script every 1 hour, but it doesn't work.
Here is the cronjob:
Code:
0 */1 * * * sh /home/sebi/.scripts/statusbar/speaktime.sh
And here is script speaktime.sh
Code:
#!/bin/bash
PATH=/bin/bash
printf "It's $(date +%R) o'clock" | espeak-ng
What have I done wrong?
You didn't read the crontab man page. A handy guide to paste into the top of your crontab for reference:
Code:
#  *   *   *   *   *      command to be executed
#  -   -   -   -   -
#  |   |   |   |   |
#  |   |   |   |   +----- day of week (0 - 6) (Sunday=0)
#  |   |   |   +------- month (1 - 12)
#  |   |   +--------- day of month (1 - 31)
#  |   +----------- hour (0 - 23)
#  +------------- min (0 - 59)
So from your example, it's going to run either at 11, or not at all. If you want it to run every hour, you use minutes as 0 (or 00), and an asterisk for every hour.
 
Old 06-10-2019, 01:46 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,267

Rep: Reputation: 580Reputation: 580Reputation: 580Reputation: 580Reputation: 580Reputation: 580
PATH consists of search directories!
Change it to
Code:
PATH=/bin:/usr/bin
Then programs like "date" is searched in /bin and /usr/bin.
You can also omit the PATH setting, trusting the cron daemon to preset a proper PATH.
 
Old 06-10-2019, 08:59 PM   #5
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 15,774
Blog Entries: 27

Rep: Reputation: 4630Reputation: 4630Reputation: 4630Reputation: 4630Reputation: 4630Reputation: 4630Reputation: 4630Reputation: 4630Reputation: 4630Reputation: 4630Reputation: 4630
Did you log in /var/log/cron? It might give you some hints.
 
Old 06-11-2019, 02:03 AM   #6
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,643

Rep: Reputation: 278Reputation: 278Reputation: 278
Cool

Quote:
Originally Posted by CoBE10 View Post
I have created cronjob which I want to execute script every 1 hour, but it doesn't work.

Here is the cronjob:

Code:
0 */1 * * * sh /home/sebi/.scripts/statusbar/speaktime.sh
And here is script speaktime.sh

Code:
#!/bin/bash
PATH=/bin/bash
printf "It's $(date +%R) o'clock" | espeak-ng
What have I done wrong?
Have you run the the script speaktime.sh in terminal to check if it's working?

If not maybe you should do chmod +x speaktime.sh, then try running crontab again.

which command do you run crontab? make sure it's not listed here: /usr/lib/cron/cron.deny

https://crontab.guru/#0_*/1_*_*_* - will run your script at minute zero past every hour.
 
Old 06-11-2019, 12:03 PM   #7
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,116

Rep: Reputation: 633Reputation: 633Reputation: 633Reputation: 633Reputation: 633Reputation: 633
Quote:
Originally Posted by CoBE10 View Post
And here is script speaktime.sh

Code:
#!/bin/bash
PATH=/bin/bash
printf "It's $(date +%R) o'clock" | espeak-ng
What have I done wrong?
As people already told you: you're destroying the correct PATH by setting it to an executable instead of (a list of) directories.
So the job cannot find espeak-ng (which probably is in /usr/bin) and NO output will be generated. Either remove the PATH= statement or set it the correct way, like
PATH=/bin:/usr/bin:/usr/local/bin
or something like that.
 
Old 06-11-2019, 12:19 PM   #8
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: Currently: openSUSE, Raspbian, Slackware. Formerly: CentOS, MacOS, Red Hat. Other: Solaris, Tru64
Posts: 1,672

Rep: Reputation: 202Reputation: 202Reputation: 202
Quote:
Originally Posted by MadeInGermany View Post
PATH consists of search directories!
Change it to
Code:
PATH=/bin:/usr/bin
Then programs like "date" is searched in /bin and /usr/bin.
You can also omit the PATH setting, trusting the cron daemon to preset a proper PATH.
Unless you're trying to run something from a non-system subdirectory. Like your account's personal bin subdirectory.

Failures with cron jobs frequently come down to wrong assumptions about the environment.

You can specify a PATH in your crontab. The top of my crontab looks like:
Code:
$ head -6 ~/crontab.MASTER
#
#  R. Turner's crontab
#
#  min, hr, dom, mon, dow, command
#
PATH=/usr/bin:/bin:/home/rnturn/bin:/usr/local/bin:/opt/app/postgresql/bin
If you write your scripts to always use fully-qualified paths, then you can omit that. In my experience, people don't. Heck I don't always. Hence my PATH in my crontab.

"crontab.MASTER"? Yes. I makes changes to that file, then issue "crontab ~/crontab.MASTER". (That way I'm sure it gets backed up every night along with the rest of the user homes. And silly errors while running "crontab -e" are less awful.)

Cheers...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
DIff B/w Crontab -l & crontab -e Tareq9959 Linux - Newbie 2 05-14-2013 09:27 AM
linux crontab vs unix crontab ytd Linux - General 2 08-09-2009 06:07 AM
replaced crontab, now should get crontab back to what it was raminn Linux - Newbie 2 10-20-2008 08:15 PM
man crontab(5) vs crontab(1) Canis Polaris Linux - Newbie 2 06-04-2008 05:03 PM
system-wide crontab in /etc/crontab ner Linux - General 2 11-18-2003 01:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:26 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration