0

I need to execute a bash script every Saturday at 2 AM on a very minimal Linux-based system that does not allow for the regular user to add cronjobs. In particular, the crontab file gets resetted to some manufacturer's default every day by the system.

I therefore need a way to keep this script running in the background forever but it needs to sleep while currentTime != Saturday, 2 AM (it's a backup script, btw).

How can I do that? Thank you to all.

  • what happens if/when that 'script running the background forever' dies or is killed (eg, host reboot)? do you have cron(tab) access on another host in the network? (idea being to have a script on said host ssh into the 'minimal Linux-based system' every Saturday @ 02:00 to run the desired command/script); have you brought this issue up with the sysadmin for the host in question (eg, disable the 'reset' of the crontab)? – markp-fuso Jun 21 '22 at 15:44
  • I am the sysadmin, actually :). It is not our choice to have the system reset the crontab file, it's just a choice of Western Digital, the manufacturer of the NAS I'm working on..... I'll give your idea to have another system SSH into the NAS and running the script a go and let you know. Thanks for the quick answer! – Niccolò Cavallini Jun 21 '22 at 15:48
  • 2
    That seems strange. As the system administrator, you should also be able to disable whatever is resetting the crontab. (Identifying what is doing that is a good question, but not on-topic here. Try unix.stackexchange, serverfault.com, or superuser.com.) – chepner Jun 21 '22 at 16:24
  • do you have access to the command `at`? – kvantour Jun 21 '22 at 16:26
  • See [Sleep until a specific time/date](https://stackoverflow.com/q/645992/4154375). To sleep from now until 2 AM on Saturday: `sleep "$(( $(date '+%s' --date='02:00 next sat') - $(date '+%s') ))"`. – pjh Jun 21 '22 at 18:54
  • @chepner Unfortunately there is a daemon that is responsible for a bunch of things on the NAS, and I cannot disable it, since some of its functions are of vital importance – Niccolò Cavallini Jun 22 '22 at 16:39

1 Answers1

0

If you can make use of at, it might be possible to automatically call a script that call's itself:

Creating a script at_periodic with the following content:

#!/usr/bin/env bash
timespec="$1"; shift
"$@"
printf "%q " "$(readlink -f -- "$0")" "$timespec" "$@" | at "$timespec"

Now it is possible to run:

$ ./at_periodic "2am Saturday" command with -- complex arguments
kvantour
  • 25,269
  • 4
  • 47
  • 72