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
  • 5
  • 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
  • 1
    Maybe using the `at` unix command? – Olivier Darrouzet Jul 16 '19 at 08:57

3 Answers3

3

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
  • 31
  • 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
2

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
  • 13,061
  • 14
  • 45
  • 83
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,195
  • 8
  • 16
  • 28
  • 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