2

I am building a unix package in which there is a script, according to the client requirements, script should run only once (not on daily basis). I am not sure how will it work? I mean, do i need to schedule it ? if yes , then I could not find any cron command for it. If no, then how will script get execute when package is being installed?

tani joshi
  • 31
  • 1
  • 7
  • can you share some info on the script? can you send part of the script? why can't the script itself, as part of the installation, run the only-once task? – nivpeled Jul 16 '19 at 08:40
  • You have to define what "once" means. If it is one per boot, some cron implementation has `@reboot` or use something like systemd. https://serverfault.com/questions/111609/how-to-run-a-cron-job-only-once – ymonad Jul 16 '19 at 08:43
  • This user had the same problem: https://stackoverflow.com/questions/5473780/how-to-setup-cron-to-run-a-file-just-once-at-a-specific-time-in-future – rmac38 Jul 16 '19 at 08:43
  • 2
    Maybe using the `at` unix command? – Olivier Darrouzet Jul 16 '19 at 08:57

4 Answers4

5

Cron is used for repetitive executions of the same command or script. If you look for a way to schedule an only once script run i would suggest you to use "at" command

Schedule script to run in 10minutes from now:

at now +10 minutes
sh <path_to_my_script>

Then ctrl+d to save

Instead of minutes you can use hours|days|weeks and so on. For reference https://linux.die.net/man/1/at

List queued jobs:

atq

To remove a given scheduled job:

atrm <job_id>

Note: please ensure you have previleges to run the the scheduled script or run it with sudo.

fabroot
  • 51
  • 2
  • `at -t now +10 minutes` is a non-standard, non-portable GNU extension to the [POSIX `at` utility](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/at.html) and is not generally available on Unix systems, and is definitely not available on [Solaris `at`](https://docs.oracle.com/cd/E26502_01/html/E29030/at-1.html#scrolltoc). – Andrew Henle Jul 30 '19 at 01:13
  • @AndrewHenle thanks for your comment. In fact i don't use solaris for a while and i don't recall using at on it, though it is available. At least since solaris 11. – fabroot Jul 30 '19 at 09:27
  • Is `at` jobs persisted after reboot? – Bolong Aug 28 '23 at 02:50
4

There are two general ways to schedule a task in Unix/Linux:

  • cron is useful for repeatable tasks.
  • at is useful for single tasks.

So, in your case, I'd use at for schedule your task.

Dominique
  • 16,450
  • 15
  • 56
  • 112
2

You can use transient systemd timers to run a command once in case at is not available in your system.

# systemd-run --on-calendar "Tue 2023-05-23 08:09:00" touch /tmp/hello
Running timer as unit: run-r7cd1a32d0fff4003a4d6fc744c41e1fe.timer
Will run service as unit: run-r7cd1a32d0fff4003a4d6fc744c41e1fe.service

# systemctl list-timers --all
NEXT                          LEFT          LAST                          PASSED      UNIT                                        ACTIVATES
Tue 2023-05-23 08:09:00 CEST  59s left      n/a                           n/a         run-r7cd1a32d0fff4003a4d6fc744c41e1fe.timer run-r7cd1a32d0fff4003a4d6fc744c41e1fe.service
...
8 timers listed.

# systemctl status run-r7cd1a32d0fff4003a4d6fc744c41e1fe.service
● run-r7cd1a32d0fff4003a4d6fc744c41e1fe.service - /usr/bin/touch /tmp/hello
   Loaded: loaded (/run/systemd/transient/run-r7cd1a32d0fff4003a4d6fc744c41e1fe.service; transient)
Transient: yes
   Active: inactive (dead)

In case you want to preserve logs for this transient timer you can use the --remain-after-exit flag. For example:

# systemd-run --remain-after-exit --on-calendar "Tue 2023-05-23 08:18:00" echo "Hello"
Running timer as unit: run-r609f6d246df54064bee769e8b2e2f976.timer
Will run service as unit: run-r609f6d246df54064bee769e8b2e2f976.service

...after executing the job...

# systemctl status run-r609f6d246df54064bee769e8b2e2f976.service
● run-r609f6d246df54064bee769e8b2e2f976.service - /usr/bin/echo Hello
   Loaded: loaded (/run/systemd/transient/run-r609f6d246df54064bee769e8b2e2f976.service; transient)
Transient: yes
   Active: active (exited) since Tue 2023-05-23 08:18:00 CEST; 3s ago
  Process: 953587 ExecStart=/usr/bin/echo Hello (code=exited, status=0/SUCCESS)
 Main PID: 953587 (code=exited, status=0/SUCCESS)

May 23 08:18:00 bbpadm-01.bbp.epfl.ch systemd[1]: Started /usr/bin/echo Hello.
May 23 08:18:00 bbpadm-01.bbp.epfl.ch echo[953587]: Hello

These are some examples you can use for the --on-calendar parameter in the command line (assuming the current time was 2012-11-23 18:15:22 and the timezone was UTC+8, for example "TZ=:Asia/Shanghai"):

  Fri 2012-11-23 11:12:13 → Fri 2012-11-23 11:12:13
      2012-11-23 11:12:13 → Fri 2012-11-23 11:12:13
  2012-11-23 11:12:13 UTC → Fri 2012-11-23 19:12:13
               2012-11-23 → Fri 2012-11-23 00:00:00
                 12-11-23 → Fri 2012-11-23 00:00:00
                 11:12:13 → Fri 2012-11-23 11:12:13
                    11:12 → Fri 2012-11-23 11:12:00
                      now → Fri 2012-11-23 18:15:22
                    today → Fri 2012-11-23 00:00:00
                today UTC → Fri 2012-11-23 16:00:00
                yesterday → Fri 2012-11-22 00:00:00
                 tomorrow → Fri 2012-11-24 00:00:00
tomorrow Pacific/Auckland → Thu 2012-11-23 19:00:00
                 +3h30min → Fri 2012-11-23 21:45:22
                      -5s → Fri 2012-11-23 18:15:17
                11min ago → Fri 2012-11-23 18:04:22
              @1395716396 → Tue 2014-03-25 03:59:56

For more information, just use man systemd-run in your computer or check:

Pigueiras
  • 18,778
  • 10
  • 64
  • 87
  • **Of course** systemd had to replace the functional, simple, [POSIX-standard](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/at.html) `at` command with an overly-complex, not-as-reliable replacement. Because what's Linux without systemd's "solution in search of a problem" needless complexity? Upvoted for pointing out the possibility though. – Andrew Henle May 24 '23 at 12:47
1

If it is only once at installing package, you do not need cron at all, just make the user execute it. Lets say you instruct the user to execute something like ./install, and include the script and that's it.

But as someone previously said, first define what is the meaning of once.

Regards.

Obsidian
  • 3,719
  • 8
  • 17
  • 30
  • This link is useful in creating postinstall script for Solaris's SVR5 `pkgadd`: https://docs.oracle.com/cd/E19253-01/817-0406/ch3enhancepkg-10289/index.html (That's an older version, as the question doesn't specify which Solaris version is applicable.) – Andrew Henle Jul 30 '19 at 01:16