KDE Task Scheduler/KCron help running bash script

After spending a couple weeks on and off with this, it’s time for some help.
I’ve searched and tried everything I can find to get this to work which should be easy,
but starting to be a colossal pain.

I have a simple bash script, it is executable, and set to run every minute

#!/bin/sh
echo "bash test"
notify-send "User Casts Resurrection"

In the scheduler, the command is
/home/major/Cron_Message/test.sh

crontab -e says this
***** /home/major/Cron_Message/test.sh

If the command is incorrect it will not let you click OK button.

According to what I’ve read, task scheduler knows the shell path and things automatically, but I’ve still tried variations of the command such as

sh /home/major/Cron_Message/test.sh
/bin/sh /home/major/Cron_Message/test.sh

as well as a few others which still let you click ok, but will not run automatically,
It will run however if you click the Run Now button.

I guess I need fresh eyes to offer something to try.

It is because you are trying to have the cron run notify-send. You need some special handling so it knows how to do it. You probably need to set the DISPLAY variable at the very least. You could try something like this:

DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /usr/bin/notify-send "User Casts Resurrection"

If that doesn’t work, do a search for “cron notify-send” and you will find a whole bunch of different options to overcome it.

2 Likes

This also provides more info: https://wiki.archlinux.org/index.php/Desktop_notifications#Bash

Additionally, you could try adding full path /usr/bin/ in front of command notify-send (and maybe echo) into your bash script:

  #!/bin/sh
  /usr/bin/echo "bash test"
  /usr/bin/notify-send "User Casts Resurrection"
1 Like

I agree with that. A long time ago, even before Yoda, my Unix mentor taught me this:
When writing a script, always use the full path name of a file you are referencing. Never use relative path names, that causes too many problems. Relative path names are OK with CLI commands, but not scripting.

OK that is my Linux tip for the day. I’ll shut up now.

Pudge

4 Likes

Ok, I’ve forgon using a script for the moment and just put
/usr/bin/notify-send "test"
in the command line in the task scheduler and set it to run every minute.

It does not work by using the timing configuration ,
It only works if you click run now.

Now I’m guessing something is amiss and not sure
what to do other than reinstall kcron.

What exactly are you trying to do? Why call notify-send after every minute? Who is/are supposed to receive the notifications?

I’m asking in hope for maybe finding out another way to solve the problem.

Also, the eos-update-notifier app uses notify-send in file /usr/bin/eos-reboot-required. You might want to look at that file how to use notify-send.

I’m using kde on my server.
I’d like to use kcron/Task Scheduler to restart certain services and send me emails on certain events using scripts.

Why am I running something every minute?
Answer- To get something to work at all for testing…If it’s not going to work in 60 seconds, it’s not going to work when I set a real time for it to do anything.
As far as notify-send goes, it’s and action that’s easy to do and see if it’s working.

Or directly as an alternative :slight_smile:
https://wiki.archlinux.org/index.php/Systemd/Timers

systemctl list-timers                                                                                      
NEXT                         LEFT                LAST                         PASSED             UNIT                         ACTIVATES       >
Mon 2020-07-20 00:00:00 CEST 2h 10min left       Mon 2020-07-13 17:49:05 CEST 6 days ago         fstrim.timer                 fstrim.service  >
Mon 2020-07-20 00:00:00 CEST 2h 10min left       Sun 2020-07-19 18:20:13 CEST 3h 29min ago       logrotate.timer              logrotate.servic>
Mon 2020-07-20 00:00:00 CEST 2h 10min left       Sun 2020-07-19 18:20:13 CEST 3h 29min ago       man-db.timer                 man-db.service  >
Mon 2020-07-20 00:00:00 CEST 2h 10min left       Sun 2020-07-19 18:20:13 CEST 3h 29min ago       shadow.timer                 shadow.service  >
Mon 2020-07-20 00:00:00 CEST 2h 10min left       Sun 2020-07-19 18:20:13 CEST 3h 29min ago       updatedb.timer               updatedb.service>
Mon 2020-07-20 18:35:32 CEST 20h left            Sun 2020-07-19 18:35:32 CEST 3h 13min ago       systemd-tmpfiles-clean.timer systemd-tmpfiles>
Sat 2020-08-01 15:00:00 CEST 1 weeks 5 days left Sat 2020-07-04 15:32:54 CEST 2 weeks 1 days ago pamac-cleancache.timer       pamac-cleancache>
Mon 2020-08-03 01:39:09 CEST 2 weeks 0 days left Wed 2020-07-15 21:23:53 CEST 4 days ago         btrfs-scrub@-.timer          btrfs-scrub@-.se>

8 timers listed.
Pass --all to see loaded but inactive timers, too.

Did you try what I suggested?

So if notify-send is not the essential part, then would you like to write (append) to a log file instead?
It is possible to monitor a log file with e.g. a terminal command

  tail -f /path/to/logfile

Maybe that could work around this particular issue?

I did, The neat thing it does is put a little timer wheel on the close button,
However whatever is going on the script is still not being called by task manager.

Are you sure it isn’t being called? notify-send from a job is tricky. Try writing some text to a file to see if it is running from the cron.

I tried this in the command line in the scheduler
/home/major/Cron_Message/test.sh >> /home/major/log.txt

the sh file has

#!/bin/sh
echo "bash test"

After one minute, nothing happens, but if I click Run Now,
it runs and write a log file that contains.
bash test

I haven’t been following this, and don’t have Plasma (which I assume kcron comes from) - but can it be done by other means? cronie comes to mind - or a systemd timer…

Are you sure the cron is running? What does this show:

systemctl list-unit-files | grep -i cron

kcron is just a front-end for cronie/cron.

$ systemctl list-unit-files | grep -i cron
cronie.service                                                            disabled        disabled

cronie is installed, but disabled so idk whats running atm

BUT, as I sit here (ordered pizza), all the sudden I saw a log file show up.
Now it’s writing bash test into that log file, but it took forever to start working.

I enabled the notify command, so now I’ll see if it starts doing something in a bit.
I would think something like this is supposed to start working right away?

1 Like

If cronie is disabled and not running, you should start and enable it.

1 Like