You are not logged in.

#1 2021-06-26 16:49:32

DJArch
Member
Registered: 2021-06-23
Posts: 19

[SOLVED] Why is my cron job not running?

As described in the Wiki I have installed the package

 cronie 

Then I did:

 systemctl enable cronie 

And the status shows it is running:

[me@frankenstein ~]$ systemctl status cronie
● cronie.service - Periodic Command Scheduler
     Loaded: loaded (/usr/lib/systemd/system/cronie.service; enabled; vendor preset: disabled)
     Active: active (running) since Sat 2021-06-26 18:33:34 CEST; 6min ago
   Main PID: 301 (crond)
      Tasks: 1 (limit: 18848)
     Memory: 2.1M
        CPU: 144ms
     CGroup: /system.slice/cronie.service
             └─301 /usr/bin/crond -n

Warning: some journal files were not opened due to insufficient permissions.

I then created my script in:

 ~/.cron/bat_notif.sh 

Which looks like:

[me@frankenstein ~]$ cat ~/.cron/bat_notif.sh 
#!/bin/bash
#Cron job to send notifications on battery level status
EF_NOW=$(cat /sys/class/power_supply/BAT0/energy_full)
EF_DESIGN=$(cat /sys/class/power_supply/BAT0/energy_full_design)
E_NOW=$(cat /sys/class/power_supply/BAT0/energy_now)

P_NOW=$(( $E_NOW * 100 /$EF_NOW))
S_NOW=$(( $EF_NOW * 100 /$EF_DESIGN))

STATUS=$(cat /sys/class/power_supply/BAT0/status)

if [[ $STATUS == "Discharging" && $P_NOW < 40 ]]
  then
    notify-send "Connect Charger" "Battery at $P_NOW%.\nMax capacity at $S_NOW%." --icon=battery-caution
fi

if [[ $STATUS == "Charging" && $P_NOW > 80 ]]
  then
    notify-send "Disconnect Charger" "Battery at $P_NOW%.\nMax capacity at $S_NOW%." --icon=battery
fi

My script is OK, because I can run it as:

 [me@frankenstein ~]$ bash ~/.cron/bat_notif.sh 

and I see the notification pop-up just as I wanted.

I edited the crontab file as follows

 [me@frankenstein ~]$ EDITOR=vim crontab -e 

and it looks like this:

 * * * * * bash ~/.cron/bat_notif.sh 

I rebooted, waited for a couple of minutes, but nothing happens. How can I check what is worng? Shouldn't this command be running every minute?

Thanks

Last edited by DJArch (2021-06-26 19:27:34)

Offline

#2 2021-06-26 17:00:59

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,439

Re: [SOLVED] Why is my cron job not running?

Try to use full paths and no underscores.
https://www.unix.com/man-page/Linux/8/RUN-PARTS/

Last edited by kokoko3k (2021-06-26 17:01:12)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#3 2021-06-26 17:17:26

DJArch
Member
Registered: 2021-06-23
Posts: 19

Re: [SOLVED] Why is my cron job not running?

I changed my filename to remove underscore as your suggested:

 mv bat_notif.sh battery.sh 

And modified my crontab to use full paths:

 * * * * * /bin/bash /home/me/.cron/battery.sh 

Still nothing. Rebooted, waited for a while, nothing.

The script does run if I do (I see the expected notification):

 [me@frankenstein ~]$ /bin/bash /home/me/.cron/battery.sh 

Last edited by DJArch (2021-06-26 18:09:18)

Offline

#4 2021-06-26 18:12:52

DJArch
Member
Registered: 2021-06-23
Posts: 19

Re: [SOLVED] Why is my cron job not running?

Some other threads on "My cron job doesn't work" suggest to make the script independent on any environment variables.

I have added

 export DISPLAY=:0 

To my script, but it still doesn't work when I run it as a cron job.

Offline

#5 2021-06-26 18:15:15

DJArch
Member
Registered: 2021-06-23
Posts: 19

Re: [SOLVED] Why is my cron job not running?

I have added the following to my crontab:

 
[me@frankenstein ~]$ crontab -l
* * * * * /bin/bash /home/me/.cron/battery.sh
* * * * * /bin/echo "cron works" >> /tmp/test_file.txt

And I do see how the other cron job updates the other file every minute. So things are set correctly, and this should be working, but for some reason my other script is not being executed.

Offline

#6 2021-06-26 18:20:53

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 24,477

Re: [SOLVED] Why is my cron job not running?

You also need to set your DBUS_SESSION_BUS_ADDRESS to the one used by your user for notify send to work, check your local environment for the current value with printenv or so.

You might want to consider using a user level systemd timer instead, which will ensure that these kind of things are set for tasks it executes.

https://wiki.archlinux.org/title/Systemd/User
https://wiki.archlinux.org/title/Systemd/Timers

Offline

#7 2021-06-26 19:26:57

DJArch
Member
Registered: 2021-06-23
Posts: 19

Re: [SOLVED] Why is my cron job not running?

V1del wrote:

You also need to set your DBUS_SESSION_BUS_ADDRESS to the one used by your user for notify send to work, check your local environment for the current value with printenv or so.

You might want to consider using a user level systemd timer instead, which will ensure that these kind of things are set for tasks it executes.

https://wiki.archlinux.org/title/Systemd/User
https://wiki.archlinux.org/title/Systemd/Timers

This did solve my problem. Thanks a lot.

Offline

Board footer

Powered by FluxBB