Linux timed task

at

if not, Start Atd Service

systemctl status atd.service
//output:
┌─[root@nedrain]─[~]
└──? $systemctl status atd.service
● atd.service - Job spooling tools
   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-06-16 10:51:39 CST; 2 days ago
 Main PID: 468 (atd)
   CGroup: /system.slice/atd.service
           └─468 /usr/sbin/atd -f

Jun 16 10:51:39 localhost.localdomain systemd[1]: Started Job spooling tools.
Jun 16 10:51:39 localhost.localdomain systemd[1]: Starting Job spooling tools...
Hint: Some lines were ellipsized, use -l to show in full.
// if the service not running, use "systemctl start atd.service" to start the service

/etc/at.allow ? /etc/at.deny (the .allow file has priority)

//if none of those two file exists, only root can use command "at"

at [option] [time]

// example of time formation
// HH:MM  02:30
// HH:MM YYYY-MM-DD 02:30 2013-07-25
// HH:MM  + [minutes | hours | days | weeks]  now + 5 minutes, 05am + 2 hours

┌─[root@nedrain]─[~]
└──? $at now + 2 minutes  // this is at command
at> echo "hello">/root/a.txt
at> <EOT>              // use ctrl + d to end the input
job 6 at Fri Jun 19 11:15:00 2020   // here‘s the output

atq (look up the timing tasks roughly)


┌─[root@nedrain]─[~]
└──? $atq
6	Fri Jun 19 11:15:00 2020 a root

at -c [task number] ( for details)

┌─[root@nedrain]─[~]
└──? $at -c 6
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
umask 22
XDG_SESSION_ID=546; export XDG_SESSION_ID
HOSTNAME=nedrain; export HOSTNAME
SHELL=/bin/bash; export SHELL
HISTSIZE=1000; export HISTSIZE
SSH_CLIENT=223.104.37.171\ 27695\ 22; export SSH_CLIENT
SSH_TTY=/dev/pts/0; export SSH_TTY
LC_ALL=en_US.UTF-8; export LC_ALL
USER=root; export USER
LS_COLORS=rs=0:di=01\;34:ln=01\;36:mh=00:pi=40\;33:so=01\;35:do=01\;35:bd=40\;33\;01:cd=40\;33\;01:or=40\;31\;01:mi=01\;05\;37\;41:su=37\;41:sg=30\;43:ca=30\;41:tw=30\;42:ow=34\;42:st=37\;44:ex=01\;32:\*.tar=01\;31:\*.tgz=01\;31:\*.arc=01\;31:\*.arj=01\;31:\*.taz=01\;31:\*.lha=01\;31:\*.lz4=01\;31:\*.lzh=01\;31:\*.lzma=01\;31:\*.tlz=01\;31:\*.txz=01\;31:\*.tzo=01\;31:\*.t7z=01\;31:\*.zip=01\;31:\*.z=01\;31:\*.Z=01\;31:\*.dz=01\;31:\*.gz=01\;31:\*.lrz=01\;31:\*.lz=01\;31:\*.lzo=01\;31:\*.xz=01\;31:\*.bz2=01\;31:\*.bz=01\;31:\*.tbz=01\;31:\*.tbz2=01\;31:\*.tz=01\;31:\*.deb=01\;31:\*.rpm=01\;31:\*.jar=01\;31:\*.war=01\;31:\*.ear=01\;31:\*.sar=01\;31:\*.rar=01\;31:\*.alz=01\;31:\*.ace=01\;31:\*.zoo=01\;31:\*.cpio=01\;31:\*.7z=01\;31:\*.rz=01\;31:\*.cab=01\;31:\*.jpg=01\;35:\*.jpeg=01\;35:\*.gif=01\;35:\*.bmp=01\;35:\*.pbm=01\;35:\*.pgm=01\;35:\*.ppm=01\;35:\*.tga=01\;35:\*.xbm=01\;35:\*.xpm=01\;35:\*.tif=01\;35:\*.tiff=01\;35:\*.png=01\;35:\*.svg=01\;35:\*.svgz=01\;35:\*.mng=01\;35:\*.pcx=01\;35:\*.mov=01\;35:\*.mpg=01\;35:\*.mpeg=01\;35:\*.m2v=01\;35:\*.mkv=01\;35:\*.webm=01\;35:\*.ogm=01\;35:\*.mp4=01\;35:\*.m4v=01\;35:\*.mp4v=01\;35:\*.vob=01\;35:\*.qt=01\;35:\*.nuv=01\;35:\*.wmv=01\;35:\*.asf=01\;35:\*.rm=01\;35:\*.rmvb=01\;35:\*.flc=01\;35:\*.avi=01\;35:\*.fli=01\;35:\*.flv=01\;35:\*.gl=01\;35:\*.dl=01\;35:\*.xcf=01\;35:\*.xwd=01\;35:\*.yuv=01\;35:\*.cgm=01\;35:\*.emf=01\;35:\*.axv=01\;35:\*.anx=01\;35:\*.ogv=01\;35:\*.ogx=01\;35:\*.aac=01\;36:\*.au=01\;36:\*.flac=01\;36:\*.mid=01\;36:\*.midi=01\;36:\*.mka=01\;36:\*.mp3=01\;36:\*.mpc=01\;36:\*.ogg=01\;36:\*.ra=01\;36:\*.wav=01\;36:\*.axa=01\;36:\*.oga=01\;36:\*.spx=01\;36:\*.xspf=01\;36:; export LS_COLORS
MAIL=/var/spool/mail/root; export MAIL
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin; export PATH
PWD=/root; export PWD
LANG=en_US.UTF-8; export LANG
PS1=\\[\\033[0\;31m\\]\\342\\224\\214\\342\\224\\200[\\[\\033[0\;39m\\]\\u\\[\\033[01\;33m\\]@\\[\\033[01\;96m\\]\\h\\[\\033[0\;31m\\]]\\342\\224\\200[\\[\\033[0\;32m\\]\\w\\[\\033[0\;31m\\]]\\n\\[\\033[0\;31m\\]\\342\\224\\224\\342\\224\\200\\342\\224\\200\\342\\225\\274\ \\[\\033[0m\\]\\[\\e[01\;33m\\]\$\\[\\e[0m\\]; export PS1
HISTCONTROL=ignoredups; export HISTCONTROL
SHLVL=1; export SHLVL
HOME=/root; export HOME
LOGNAME=root; export LOGNAME
SSH_CONNECTION=223.104.37.171\ 27695\ 172.19.4.50\ 22; export SSH_CONNECTION
LC_CTYPE=UTF-8; export LC_CTYPE
LESSOPEN=\|\|/usr/bin/lesspipe.sh\ %s; export LESSOPEN
XDG_RUNTIME_DIR=/run/user/0; export XDG_RUNTIME_DIR
cd /root || {
	 echo ‘Execution directory inaccessible‘ >&2
	 exit 1
}
${SHELL:-/bin/sh} << ‘marcinDELIMITER65959aa8‘
echo "hello">/root/a.txt      // only this line is your code

marcinDELIMITER65959aa8

cron

also first start your crond service (if not started)

//

also, file /etc/cron.allow & file /etc/cron.deny

┌─[root@nedrain]─[~]
└──? $ls /etc/cron*
/etc/cron.deny  /etc/crontab

/etc/cron.d:
0hourly  sysstat

/etc/cron.daily:
logrotate  man-db.cron

/etc/cron.hourly:
0anacron

/etc/cron.monthly:

/etc/cron.weekly:

crontab -e [task]

// here is the file /etc/crontab
/*
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed */

// there are special symbols
// * means random time , so "* * * * *" will let the command execute every minute
// */n means executing command every n minutes
// 0 8, 12, 16 * * * means executing command at 08:00 and 12:00 and 16:00
// 0 5 * * 1-6 means executing command at 05:00 from Monday to Saturday
 

crontab -e
//* * * * * root echo 1111 >> /root/a.txt   // this is a wrong example, don‘t add a root 
* * * * * echo 1111 >> /root/a.txt

 
// use "crontab -l " to query
┌─[root@nedrain]─[~]
└──? $crontab -l
* * * * *  echo 1111 > /root/a.txt

and if you wanna change , keep using "crontab -e" to edit, or use "crontab -r" to remove all

if you use "crontab -e", you can‘t let others do the task, but by editing /etc/crontab, you can let others do the job for you

// here is the file /etc/crontab
/*
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed */

Linux timed task

上一篇:PuTTY与Linux断开连接-重连后关闭node应用


下一篇:Memcache 查看列出所有key方法