[Solved] Cron Job Failing

Quick to answer questions about finding your way around Linux Mint as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums in the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
johnywhy

[Solved] Cron Job Failing

Post by johnywhy »

My script /usr/local/bin/banana.sh runs just fine, when called directly from command-line.

Code: Select all

#!/bin/bash
notify-send "Banana"
But crontab isn't executing. crontab was created at non-root command-line

Code: Select all

crontab -e
*/1 * * * * banana.sh
This also fails:

Code: Select all

*/1 * * * * /usr/local/bin/banana.sh
task manager shows cron -f is running.

i'm seeing a lot of "Ignored relative path" errors in syslog, but that seems unrelated.

Code: Select all

Aug 20 05:38:18 minty-boost ureadahead[337]: ureadahead:tty: Ignored relative path
Aug 20 05:38:18 minty-boost ureadahead[337]: ureadahead:ttyS21: Ignored relative path
Aug 20 05:38:18 minty-boost ureadahead[337]: ureadahead:: Ignored relative path
Aug 20 05:38:18 minty-boost ureadahead[337]: ureadahead:sys: Ignored relative path
Aug 20 05:38:18 minty-boost ureadahead[337]: ureadahead:devices: Ignored relative path
Aug 20 05:38:18 minty-boost ureadahead[337]: ureadahead:platform: Ignored relative path
Aug 20 05:38:18 minty-boost ureadahead[337]: ureadahead:serial8250: Ignored relative path
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
ralplpcr
Level 6
Level 6
Posts: 1096
Joined: Tue Jul 28, 2015 10:11 am

Re: Cron Job Failing

Post by ralplpcr »

Not quite sure I follow the */1 at the front of your statement - are you trying to make it execute every odd minute, or every minute?

I've got 2 possible suggestions. At work now, and can't try it out myself, but I'd think you'd want to include the full path. You *may* also need to tell it to run in bash...... have you verified that your "banana.sh" file has the executable bit set?

Every even minute:

Code: Select all

*/2 * * * * /usr/bin/banana.sh >/dev/null 2>&1
Including the instruction to run with bash:

Code: Select all

*/2 * * * * sh /usr/bin/banana.sh >/dev/null 2>&1
johnywhy

Re: Cron Job Failing

Post by johnywhy »

as you can see in my script above, i'm not sending output to null. But i just tried that, with all asterisk's, and preceded by sh. Still not firing.

Code: Select all

* * * * * sh /usr/local/bin/banana.sh >/dev/null 2>&1
*/1 * * * * means run every minute.
https://tecadmin.net/crontab-in-linux-w ... -schedule/

* * * * * should also work
https://www.cyberciti.biz/faq/how-to-ru ... linuxunix/

(Technically, */2 doesn't mean "every even minute". It means "every 2nd minute" or "every 2 minutes". */1 means "every 1 minutes")

THX
gm10

Re: Cron Job Failing

Post by gm10 »

johnywhy wrote: Mon Aug 20, 2018 2:29 pm My script /usr/local/bin/banana.sh runs just fine, when called directly from command-line.

Code: Select all

#!/bin/bash
notify-send "Banana"
But crontab isn't executing. crontab was created at non-root command-line

Code: Select all

crontab -e
*/1 * * * * banana.sh
crontab is executing just fine. But you're forgetting that cron isn't part of your desktop session. It just runs under a minimal environment. To send messages to your desktop it needs to have access to the dbus interface.

Add this as the first command in your script:

Code: Select all

export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME mate-session)/environ)
(replace mate-session with whatever desktop session manager you are running)
User avatar
MrEen
Level 23
Level 23
Posts: 18343
Joined: Mon Jun 12, 2017 8:39 pm

Re: Cron Job Failing

Post by MrEen »

Cron doesn't know /usr/local/bin/ by default.

Use /usr/bin/ or /usr/sbin instead.

EDIT: Nevermind. Don't know what I was thinking.
gm10

Re: Cron Job Failing

Post by gm10 »

MrEen wrote: Mon Aug 20, 2018 3:45 pm Cron doesn't know /usr/local/bin/ by default.

Use /usr/bin/ or /usr/sbin instead.

EDIT: Nevermind. Don't know what I was thinking.
Why, it's correct except it doesn't know /usr/sbin, either. Best practice is to either always use the full path in crontab or to define PATH at the beginning of your crontab.
johnywhy

Re: Cron Job Failing

Post by johnywhy »

SCRIPT
DBUS_SESSION_BUS_ADDRESS works! THX gm10.

Code: Select all

#!/bin/bash
export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME xfce4-session)/environ)
notify-send "Banana"
gm10 wrote: Mon Aug 20, 2018 3:39 pmyou're forgetting that cron isn't part of your desktop session.
ha-ha, i can't forget something i didn't know :D

Is the DBUS_SESSION_BUS_ADDRESS line needed only if i'm interacting with the desktop?


SH
At command-line, works with or without sh.
But without sh, i get warning. Why?

Code: Select all

$ sh banana.sh
$ banana.sh
/usr/local/bin/banana.sh: line 2: warning: command substitution: ignored null byte in input

CRONTAB
null redirect isn't needed. Works:

Code: Select all

* * * * * /usr/local/bin/banana.sh

DIRECTORY
Don't use these directories. Leave them for package-managed executables.
/usr/bin, /sbin and /bin

If you need the script for all users on your system (but you can also use this for one user), stick it in /usr/local/bin/. One advantage: this directory is already in your PATH so there is no need to edit files.
https://askubuntu.com/a/465162
However, even if i put the script in /usr/local/bin/, i notice that i still must include the full path in the crontab. i would prefer if that wasn't necessary.

THX
gm10

Re: Cron Job Failing

Post by gm10 »

johnywhy wrote: Mon Aug 20, 2018 4:48 pm Is the DBUS_SESSION_BUS_ADDRESS line needed only if i'm interacting with the desktop?
It's what you need if you want to interact with another process.

Maybe have a look here, although it may just be confusing:
https://en.wikipedia.org/wiki/D-Bus
johnywhy wrote: Mon Aug 20, 2018 4:48 pm SH
At command-line, works with or without sh.
But without sh, i get warning. Why?

Code: Select all

$ sh banana.sh
$ banana.sh
/usr/local/bin/banana.sh: line 2: warning: command substitution: ignored null byte in input
ah, sorry, your script references bash, hadn't paid attention. the default shell for cron is sh. for bash modify like this to get rid of the warning:

Code: Select all

export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME xfce4-session)/environ | tr \\0 \\n) 
Alternatively remove the #!/bin/bash line from your script or explicitly reference #!/bin/sh instead.
johnywhy wrote: Mon Aug 20, 2018 4:48 pm DIRECTORY
[...]
However, even if i put the script in /usr/local/bin/, i notice that i still must include the full path in the crontab. i would prefer if that wasn't necessary.
Run this and add the output (not the command itself) to the top of your user crontab:

Code: Select all

echo PATH=$(printenv PATH)
That way your crontab has access to the same path as you have in a terminal.
johnywhy

Re: Cron Job Failing

Post by johnywhy »

gm10 wrote: Mon Aug 20, 2018 5:20 pm
johnywhy wrote: Mon Aug 20, 2018 4:48 pm Is the DBUS_SESSION_BUS_ADDRESS line needed only if i'm interacting with the desktop?
It's what you need if you want to interact with another process.
A desktop process?
default shell for cron is sh
so bash and sh are two different script interpreters? :)
Run this and add the output (not the command itself) to the top of your user crontab:

Code: Select all

echo PATH=$(printenv PATH)
Neat! However, in this case, it's less effort to just type the path on the command itself :)
Would be great if crontabs got path globally, without having to type it into every crontab.

THX
gm10

Re: Cron Job Failing

Post by gm10 »

johnywhy wrote: Mon Aug 20, 2018 5:48 pm
gm10 wrote: Mon Aug 20, 2018 5:20 pm
johnywhy wrote: Mon Aug 20, 2018 4:48 pm Is the DBUS_SESSION_BUS_ADDRESS line needed only if i'm interacting with the desktop?
It's what you need if you want to interact with another process.
A desktop process?
I'm not sure what you mean by that but in case of doubt the answer is "yes, but not limited to". Is notify-send a desktop process?
johnywhy wrote: Mon Aug 20, 2018 5:48 pm
default shell for cron is sh
so bash and sh are two different script interpreters? :)
https://en.wikipedia.org/wiki/Unix_shell
johnywhy wrote: Mon Aug 20, 2018 5:48 pm
Run this and add the output (not the command itself) to the top of your user crontab:

Code: Select all

echo PATH=$(printenv PATH)
Neat! However, in this case, it's less effort to just type the path on the command itself :)
Would be great if crontabs got path globally, without having to type it into every crontab.
I think you misunderstand. I said to put the PATH into the crontab, not the script. That way it's available for all your cron jobs.
johnywhy

Re: Cron Job Failing

Post by johnywhy »

gm10 wrote: Mon Aug 20, 2018 6:30 pm I think you misunderstand. I said to put the PATH into the crontab, not the script. That way it's available for all your cron jobs.
I'm to put the output of this into the top of THIS crontab, correct? How will that be seen by other crontabs?

echo PATH=$(printenv PATH)
gm10

Re: [Solved] Cron Job Failing

Post by gm10 »

What other crontabs? Every user only has a single one. And other users have a different PATH so they wouldn't want yours.
johnywhy

Re: [Solved] Cron Job Failing

Post by johnywhy »

gm10 wrote: Mon Aug 20, 2018 6:50 pm What other crontabs? Every user only has a single one.
oh, right, ok.

according to the post i linked above, a script for all users can go into /usr/local/bin/
Last edited by johnywhy on Tue Aug 21, 2018 12:10 pm, edited 1 time in total.
ralplpcr
Level 6
Level 6
Posts: 1096
Joined: Tue Jul 28, 2015 10:11 am

Re: Cron Job Failing

Post by ralplpcr »

johnywhy wrote: Mon Aug 20, 2018 3:30 pm */1 * * * * means run every minute.
https://tecadmin.net/crontab-in-linux-w ... -schedule/
That's a new one to me - - I've only ever seen it expressed as * * * * * before.
Learn something new every day! :)
Locked

Return to “Beginner Questions”