How to script at job creation?
Karl Auer
kauer at biplane.com.au
Thu Dec 10 11:45:54 UTC 2020
On Thu, 2020-12-10 at 11:18 +0100, Bo Berglund wrote:
> I need at rather than cron because the at job creation saves the user
> and the current dir and it seems like also the user environment at
> the time of creation.
Each user has a crontab, and in the main crontab you can specify a
user, so that's half the "problem" solved. You can change to any
desired directory as part of running any cronjob, so that part of the
problem is also solved. What specifically do you need from the user
environment?
> With cron it gets much more complex...
Generally not, unless the job needs interactive input, which would be a
problem with at too, so...
> So I would like to create a script I can run in the target dir and it
> will create the 12 different at jobs in one command execution.
>
> Can this be done at all?
Absolutely. If all the scripts are run as the same user, you only need
one master script, and it simply runs all the other scripts. Run the
master script from cron. If any of the scripts have output, capture it
with redirection to a file. If the scripts take a long time to run, you
might like to run then simultaneously by adding "&" at the end of each
individual script invocation. Or capture all the output from all the
scripts by redirecting the master script's output.
You might have to twiddle it because it is off the top of my head, but
a line something like this in crontab will run master.sh:
- every day at 5:05AM
- as fred
- from fred's home directory
- after changing to /opt
- with MYVAR set to the value "thingy":
5 5 * * * fred ( cd /opt ; export MYVAR=thingy ; ~/master.sh ) > 2>
/tmp/log.txt 2>&1
Or put the same line, minus the "fred", into fred's crontab.
And of course you can also pass parameters to the script, or load
information from files or do whatever else a script, running as fred,
could normally do.
If the scripts are not supposed to all run at the same time, you can
either put a suitable "sleep" command between the lines in the master
script or (much better) have one cron job file for each script.
Regards, K.
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer (kauer at biplane.com.au)
http://www.biplane.com.au/kauer
GPG fingerprint: 2561 E9EC D868 E73C 8AF1 49CF EE50 4B1D CCA1 5170
Old fingerprint: 8D08 9CAA 649A AFEF E862 062A 2E97 42D4 A2A0 616D
More information about the ubuntu-users
mailing list