Run cron once on after app update
-
@Lanhild said in Run cron once on after app update:
How would I go about running a cron in an application after its update?
There is no hook for update as such and most likely this is not the desired way to code either. Just having a simple update hook will cause issues - the cron could crash halfway (should the hook be called next restart?), clone/restore/import lifecycles etc.
Similar to database migration scripts, your cron must be capable of being run and also figuring out that it has already done something. Migration scripts usually do this by tracking what changes have been completed in the last run (for example, a simple version number). The cron script can do the same. Save the state in some file in /app/data and then load it before it runs to check if it needs to do something.
You can run the cron as a
@service
or@reboot
. See https://docs.cloudron.io/apps/#cron -
@Lanhild said in Run cron once on after app update:
Also, in the same cronjob, how can I restart my application?
The cronjob runs in the same pid space as the app itself. Meaning, the app can be controlled by the cronjob using linux signals. Maybe kill it or send it some friendly signal to reload/restart. If your app uses supervisor, you can just do
supervisor restart app
. -
-