OpenWrt Forum Archive

Topic: how to add crontab as a dependency for your package

The content of this topic has been archived on 6 Sep 2015. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I have created a custom package, which updates the crontabs file.

This is how I add the entry in the post install script
define Package/alerts/postinst
    #!/bin/sh
    # check if we are on real system
    if [ -z "$${IPKG_INSTROOT}" ]; then
        echo "Adding to crontab"
        if [ ! -f /etc/crontabs/root ]; then
            touch /etc/crontabs/root
        fi
        echo "adding cron"
        echo '*/1 * * * * alerts' >> /etc/crontabs/root
        /etc/init.d/cron reload
        echo "added cron"
fi
exit 0
endef

When I manually install the package separately it works fine.

But when I install it into the firmware (including the package in buildroot menuconfig), there is no crontab entry.
I think package is getting installed into this firmware before the crontab utility is.

Is there any way I can get this crontab entry working for my package?


P.S.:Not sure if I'm able to convey my question properly. Please let me know if more clarifications are necessary.

Your "check if we're on a real system" test is false in the buildroot environment, therfore the code never gets executed.
I usually suggest the following solution which covers all possible variants (package built into image, package installed via opkg):

* Ship a file "/etc/uci-defaults/99_add_alerts_to_crontab" (name does not matter) which contains the code editing /etc/crontabs/root (the touch is not needed by the way, ">>" will create the file if needed):

#!/bin/sh

echo "Adding to crontab"
echo '*/1 * * * * alerts' >> /etc/crontabs/root
/etc/init.d/cron reload

* In your postinstall do the following:

define Package/alerts/postinst
#!/bin/sh
# check if we are on real system
if [ -z "$${IPKG_INSTROOT}" ]; then
  (. /etc/uci-defaults/99_add_alerts_to_crontab)
  rm -f /etc/uci-defaults/99_add_alerts_to_crontab
fi
exit 0
endef

When the package is built into the image, the postinst is never run but the /etc/uci-defaults/ script is executed and deleted on the first boot the firmware, like a run-once task in windows.

When the package is is installed with opkg, the postinstall section will execute the uci-defaults script and remove it to make the changes effective immediately, without the need for another reboot.

You should also consider changing your crontab echo line to something like that:

grep -sq /usr/bin/alerts /etc/crontabs/root || echo "*/1 * * * *  /usr/bin/alerts" >> /etc/crontabs/root

The above will...
  * ensure that no duplicate lines are added in case the entry already exists (e.g. when a config backup was previously restored by the user)
  * use an absolute path to your command because the environment might not be fully initialized

This is exactly what I wanted. And things are working perfectly!

Thanks a lot. Really appreciate your help.

The discussion might have continued from here.