Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 558  / 1 Year ago, tue, march 28, 2023, 11:58:20

I want to run a cron job to wipe files for a ephemeral user on Ubuntu desktop 20.04. Since the system won't be powering fully off regularly(for which I could just use @reboot /path/to/my_script.sh ), is there a way to schedulae a cron job to run when the computer is suspended? Everything on Google I'm seeing is related to running cron jobs while the computer is suspended, but I'd like to use suspend, or waking as the trigger for this cron job.


More From » cron

 Answers
5

That kind of trigger is not what cron is designed for. Cron is a clock-based trigger. @reboot is a convenience added to cron.


For suspend/remove jobs, one solution is to use power management.



  • See man pm-suspend

  • Put your job in the /etc/pm/sleep.d/ directory.

  • It's not a cron job. You place complete scripts in the directory.


Use case to determine what occurs on sleep, and what occurs on resume:


case "${1}" in
suspend)
suspend_actions
;;
resume)
resume_actions
;;
esac

Another similar solution is to use systemd.



  • The main difference is to use /lib/systemd/system-sleep/ instead of /etc/pm/sleep.d/


Both of these methods can arguably be considered a bit hacky: Don't use them for real applications, nor for long delays to suspending. Use inhibit instead.


[#1751] Wednesday, March 29, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
laceanz

Total Points: 86
Total Questions: 113
Total Answers: 104

Location: Australia
Member since Thu, Jan 26, 2023
1 Year ago
;