Tutorial: Scheduled tasks in CoreELEC

No anacrontab.

Try to use putty for ssh conection and execute crontab -e.

@vpeter

Thanks, PuTTY did open the crontab.

Now, can you tell me if the command #systemctl disable service.kodi is correct?

Tried from PuTTY. This isn’t working: no effect !
I want to experiment with shutting down Kodi via SSH. I am assuming this will still keep the Linux part running?

Commands are stop and start.

systemctl stop kodi
systemctl start kodi

Oh thanks.
But why doesn’t the above command work, which I found from online at the Arch Linux wiki?

No idea :innocent:

What do you want to achieve? Stop kodi permanently? Then use paramater mask to disable service and unmask to enable back.

Yes.

I would like to stop Kodi after some specified time period, even when I have rebooted the tv box in between. Think they call it a persistent command.

It’ll be nice to have kodi working again too, on demand.

The problem with the systemctl stop kodi command is that kodi is back after reboot.

How to do it?
Sorry I am a Linux noob. What is masking here?

You would need some scripting to achieve this what you want. Cron doesn’t support such functionality.

And why do you need to stop kodi?

Maybe you like my automatic daily restart using cron, maybe this solves some of your doubts, on Mondays and Fridays a full backup runs:

file /storage/.config/scripts/crontab.conf:

20 7 * * Mon /storage/.config/scripts/backup-and-reboot.sh
20 7 * * Tue systemctl reboot
20 7 * * Wed systemctl reboot
20 7 * * Thu systemctl reboot
20 7 * * Fri /storage/.config/scripts/backup-and-reboot.sh
20 7 * * Sat systemctl reboot
20 7 * * Sun systemctl reboot

file /storage/.config/scripts/backup-and-reboot.sh:

#!/bin/sh
# stop main services
systemctl stop kodi
# systemctl stop service.softcam.oscam
# systemctl stop service.tvheadend42
systemctl stop service.tvheadend43
# systemctl stop entware
# save logs for entware servers
mv /opt/var/log/messages /opt/var/log/messages.old
mv /opt/var/log/zerotier-one.log /opt/var/log/zerotier-one.log.old
# full backup
mv /media/EXT-HDD/Backups/full-backup.tar /media/EXT-HDD/Backups/full-backup.old.tar
tar cvf  /media/EXT-HDD/Backups/full-backup.tar /storage/.kodi /storage/.cache /storage/.config /storage/.opt
systemctl reboot

All of this is launched from /storage/.config/autostart.sh with the command:
crontab /storage/.config/scripts/crontab.conf

You can use commas in crontab timers:
20 7 * * 1,5 /storage/.config/scripts/backup-and-reboot.sh
20 7 * * 2,3,4,6,7 systemctl reboot

1 Like

Is there anyway I can bind a script to run with a keyboard press (F1-F12, for instance)?
I want to reboot from nand, to boot to android and it’s a fairly simple command, just need help tying it to a keyboard press.
Thx.

You don’t need 3 commands for this particular case of epg updating. The same result could be reached with just one:

wget -q -O - http://url_of_your_guide/guide.xml.gz | gunzip > /storage/path_to_your_guide_file/guide.xml

And it could be placed directly to crontab. No additional directories and scripts needed.

Hello:

I wanted to ask a “parallel question”

When I create a .sh file like:
“test.sh” with this inside:

#!/bin/sh
echo “Hello world!”

in /storage/downloads

and try to execute it (chmod +x, etc.):

CoreELEC:~/downloads # ./test.sh
sh: ./test.sh: not found
CoreELEC:~/downloads #

But THE SAME FILE, with sh, is executed perfectly:
CoreELEC:~/downloads # sh test.sh
Hello world!
CoreELEC:~/downloads #

And, when I “remove” the part:

#!/bin/sh

and I leave alone the part:

echo “Hello world!”

in the file “test.sh”, then:
CoreELEC:~/downloads # ./test.sh
Hello world!
CoreELEC:~/downloads #

So, for any reason, the part:

#!/bin/sh

makes the test.sh file fail to execute using: ./test.sh

CoreELEC:~/downloads # which sh
/opt/bin/sh
CoreELEC:~/downloads # which bash
/opt/bin/bash
CoreELEC:~/downloads #

Maybe entware (.opt, etc.) is influencing this?

Any “solution” for this?

Now using CoreELEC 20.0, ng, X905X3.

But /bin/sh is symbolic link to /bin/bash.

Anyway, it works here without entware installed and with entware installed with bash.
Maybe you have some strange character on first line in script file.

Post the content you get by command

hexdump -C test.sh

YOU ARE RIGHT!

CoreELEC:~/downloads # hexdump -C test.sh
00000000 23 21 2f 62 69 6e 2f 73 68 0a 65 63 68 6f 20 e2 |#!/bin/sh.echo .|
00000010 80 9c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 e2 80 |…Hello world!..|
00000020 9d |.|
00000021
CoreELEC:~/downloads #

dos2unix is useful here:

CoreELEC:~/downloads # ./test.sh
-sh: ./test.sh: /bin/sh: bad interpreter: Text file busy
CoreELEC:~/downloads # dos2unix test.sh
CoreELEC:~/downloads # ./test.sh
“Hello world!”
CoreELEC:~/downloads #

Perhahs dos2unix is necessary here because I create the script from samba / Windows 10?

You need to save file as UNIX formatted. But in your case it seems it was unicode with some strange characters in. Like 0x9c and 0x9d.

Thanks. I don’t know exactly the cause, but now we have a solution.

Kind regards

1 Like

Hi,

I followed the thread just to create a simple task scheduler for restarting a Docker container on every 6 hours, but the ‘‘docker’’ command has not been found in crontab.
My sh script is so simple:

#!/bin/sh

docker restart container

This is the entry in crontab:

30 */6 * * * sh /storage/.config/scripts/container-restart.sh

This is the error :

CoreELEC crond[4594]: USER root pid 5492 cmd sh /storage/.config/scripts/container-restart.sh
CoreELEC crond[4594]: /storage/.config/scripts/container-restart.sh: line 3: docker: not found

Could please help me to resolve this issue.Thanks in advance

In file container-restart.sh add this on top before your commands

source /etc/profile

or use full path to the binaries.

1 Like

Wooool

What a quick and helpful reply. It’s works wright now :grinning: Thank you so much Sir. You saved my day.

By the way…I tried with the path but without any success.
I inserted this one:

PATH=/storage/.kodi/userdata/addon_data/service.system.docker/docker

Maybe

export PATH=$PATH:/storage/.kodi/addons/service.system.docker/bin

1 Like