Tutorial: Using ‘cron’, ‘crontab’ and ‘sh’ in CoreELEC
I have seen some examples of implementation of tasks scheduled in kodi, libreelec and linux, but all are difficult to understand for a novice user in linux programming like myself. This has been written by a rookie for rookies.
I will give an example of scheduled tasks that I have implemented in my CoreELEC device:
1.- Check that ‘cron’ is activated in Settings->Services->Cron
2.- Open an SSH terminal and create the scripts
directory with the commands:
(create directory in .config, this allows it to be saved in CoreELEC backups)
mkdir /storage/.config/scripts
(change attributes)
chmod 775 /storage/.config/scripts
(create a link in ‘storage’ to be able to access the new scripts directory from kodi)
ln -s /storage/.config/scripts /storage/scripts
(remember that from now /storage/.config/scripts is the same as /storage/scripts)
3.- The scheduled tasks will be in shell files in the scripts directory. Examples:
3.1.- File: /storage/scripts/load_epgguide
#!/bin/sh
#remove old guide
$(rm '/storage/.config/scripts/guide')
#load new zipped epg guide
$(wget -O '/storage/.config/scripts/guide.gz' 'http://xxx.xxx.xxx/xxx.gz')
#unzip epg guide
$(gzip -d -f '/storage/.config/scripts/guide.gz')
3.2.- File: /storage/scripts/update_duckdns
#!/bin/sh
#update dynamic dns for external access
$(curl 'https://xxx.xxx.xxx/update/<ddns_name>/<ddns_key>')
4.- All shell files in the ‘scripts’ directory will have execution permission. Examples:
chmod 755 /storage/scripts/load_epgguide
chmod 755 /storage/scripts/update_duckdns
5.- To execute any script we will use the sh command. Examples:
sh /storage/scripts/load_epgguide
sh /storage/scripts/update_duckdns
6.- To execute the scripts as scheduled tasks we will use the command:
crontab -e
, and we will add lines as in the following example:
...
5 5 * * * sh /storage/.config/scripts/load_epgguide
10 5 * * * sh /storage/.config/scripts/update_duckdns
...
(load_epgguide will run every day at 5 and 5 in the morning, and update_duckdns will run at 5 and 10 in the morning)
This is all!