View Single Post
  #1   (View Single Post)  
Old 13th May 2021
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,128
Default Scheduling jobs with at(1) instead of cron(8)

Many are familiar with cron(8) to schedule jobs for execution in the future and/or at regular intervals.

Quote:
Originally Posted by cron(8)
The cron daemon schedules commands to be run at specified dates and times. Commands that are to be run periodically are specified within crontab(5) files. Commands that are only to be run once are scheduled via the at(1) and batch(1) commands.
The man page at(1) tersely describes what it does:
Code:
NAME
     at, batch - queue, examine or delete jobs for later execution
at(1) can either reads jobs from the standard input or from a file.

A simple example, using 'stdin', to have it execute echo Hello ; ls -l ; echo Done with ls! at about three minutes from now:

Code:
nedrag#  echo 'echo Hello ; ls -l ; echo Done with ls!' | at + 3 minutes

commands will be executed using /bin/ksh
job 1620879120.c at Thu May 13 06:12:00 2021
List your submitted job(s) with the -l option:

Code:
nedrag# at -l

1620879120.c    Thu May 13 06:12:00 2021
Use the -c option and the job identifier to view the job details:

Code:
nedrag# at -c 1620879120.c

#!/bin/sh
# atrun uid=0 gid=66
# mail                          adriaan 0
umask 22
export LOGNAME=root
export HOME=/root
export OLDPWD=/root/.ssh
export PWD=/root
export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/sbin:/usr/local/bin
export USER=root
cd /root || {
         echo 'Execution directory inaccessible' >&2
         exit 1
}
/bin/ksh << '_END_OF_AT_JOB'
echo Hello ; ls -l ; echo Done with ls!

_END_OF_AT_JOB
The mail reporting the output:
Code:
Date: Thu, 13 May 2021 06:12:01 +0200 (CEST)
From: Atrun Service <root@nedrag.siralas.nl>
To: adriaan@nedrag.siralas.nl
Subject: Output from "at" job

Your "at" job on nedrag.siralas.nl
"/var/cron/atjobs/1620879120.c"

produced the following output:

Hello
total 380
-rw-r--r--  1 root  wheel      87 Apr 19 18:16 .Xdefaults
-rw-r--r--  1 root  wheel     578 Apr 19 18:16 .cshrc
-rw-r--r--  1 root  wheel      94 Apr 19 18:16 .cvsrc
-rw-r--r--  1 root  wheel     104 May 13 03:43 .exrc
-rw-r--r--  1 root  wheel       8 May 11 23:55 .forward
-rw-r--r--  1 root  wheel     328 Apr 19 18:16 .login
-rw-r--r--  1 root  wheel     526 May 13 03:27 .profile
Done with ls!
An example of a job specification in a file:

Code:
nedrag# cat MySSHinstuders.cmd

ls -l /var/log/authlog
echo ----------------------------
grep 'Invalid user' /var/log/authlog | awk '{ print $3, $8, $10, $11, $12  }' | column -t
Submission with the -f option:
Code:
nedrag# at -f MySSHinstuders.cmd +3 minutes
                                                                                                             
commands will be executed using /bin/ksh
job 1620882180.c at Thu May 13 07:03:00 2021
Reviewing the submitted task with -c:

Code:
nedrag# at -c 1620882180.c

#!/bin/sh
# atrun uid=0 gid=66
# mail                          adriaan 0
umask 22
export LOGNAME=root
export HOME=/root
export OLDPWD=/root/.ssh
export PWD=/root
export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/sbin:/usr/local/bin
export USER=root
cd /root || {
         echo 'Execution directory inaccessible' >&2
         exit 1
}
/bin/ksh << '_END_OF_AT_JOB'
ls -l /var/log/authlog
echo ---------------------------
grep 'Invalid user' /var/log/authlog | awk '{ print $3, $8, $10, $11, $12  }' | column -t


_END_OF_AT_JOB
The resulting email:

Code:
Date: Thu, 13 May 2021 07:03:01 +0200 (CEST)
From: Atrun Service <root@nedrag.siralas.nl>
To: adriaan@nedrag.siralas.nl
Subject: Output from "at" job

Your "at" job on nedrag.siralas.nl
"/var/cron/atjobs/1620882180.c"

produced the following output:

-rw-r-----  1 root  wheel  27842 May 13 05:11 /var/log/authlog
---------------------------
01:00:23  tesco         193.112.127.245  port  34052
01:00:28  eaglewiz      39.109.116.175   port  41606
01:01:07  mfu           103.72.172.20    port  36002
01:01:40  cele          39.109.116.175   port  56122
01:02:10  admin         1.226.12.132     port  58152
01:02:51  msda          132.232.105.237  port  38760
01:02:57  oracle        138.68.139.104   port  44682
01:03:22  chenqn        193.112.127.245  port  41462
01:07:17  splunk        177.234.172.10   port  57536
01:07:50  http          103.72.172.20    port  54996
01:08:49  lava2         138.68.139.104   port  35004
01:09:04  fancong       1.226.12.132     port  57830
01:09:13  fancong       132.232.105.237  port  57392
01:09:27  thinker       193.112.127.245  port  56292
01:09:48  gymnast       103.73.102.247   port  50788
01:10:16  zhangn        177.234.172.10   port  40452
01:10:23  karl          3.24.138.187     port  58120
01:11:21  neww          1.226.12.132     port  38900
01:12:21  liq           103.72.172.20    port  39432
01:12:24  debian        193.112.127.245  port  35482
01:12:29  prueba1       132.232.105.237  port  38512
01:13:01  javier        177.234.172.10   port  51572
01:14:31  neww          103.72.172.20    port  45778
01:14:40  derek         138.68.139.104   port  53558
01:15:25  ellen         193.112.127.245  port  42898
01:15:48  mfu           132.232.105.237  port  47832
01:16:46  bobby         103.72.172.20    port  52122
01:17:11  oracle        103.73.102.247   port  37120
01:18:13  dhou          1.226.12.132     port  38580
01:18:43  wwwrun        177.234.172.10   port  45606
01:18:56  zhaoqiqi      132.232.105.237  port  57158
01:19:01  pt            103.72.172.20    port  58466
01:20:34  chenph        1.226.12.132     port  47878
01:20:42  paula         138.68.139.104   port  43882
01:21:17  testbelkofsi  103.72.172.20    port  36580
01:21:17  testbelkofsi  103.72.172.20    port  36580
01:21:25  xqh           193.112.127.245  port  57726
See the -t option of at(1) to learn about the many alternative methods to specify the time and date.

Have fun with the Unix way to get things done
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump

Last edited by J65nko; 13th May 2021 at 06:08 AM.
Reply With Quote