iT邦幫忙

第 12 屆 iT 邦幫忙鐵人賽

0
自我挑戰組

30天菜鳥學 Linux 系列 第 47

第47天-學習一次性排程工作 at

今天進度 : 鳥哥私房菜 - 第十五章、例行性工作排程(crontab)

使用 systemctl status atd 查詢狀態

test@test:~$ systemctl status atd
● atd.service - Deferred execution scheduler
     Loaded: loaded (/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-10-20 03:15:49 UTC; 1min 38s ago
       Docs: man:atd(8)
   Main PID: 70627 (atd)
      Tasks: 1 (limit: 2205)
     Memory: 376.0K
     CGroup: /system.slice/atd.service
             └─70627 /usr/sbin/atd -f

Oct 20 03:15:49 test systemd[1]: Starting Deferred execution scheduler...
Oct 20 03:15:49 test systemd[1]: Started Deferred execution scheduler.

結合 echo sleep 模擬 5 秒後創建檔案

test@test:~$ ls # 目錄下沒有檔案
test@test:~$ echo "sleep 5 ; touch test.txt" | at now
warning: commands will be executed using /bin/sh
job 2 at Tue Oct 20 03:25:00 2020
# 等待 5 秒後檔案被自動創建
test@test:~$ ls
test.txt

當下發送全 users 訊息

echo "wall 'HI IT幫'" | at now
test@test:~$ date
Tue 20 Oct 2020 03:45:45 AM UTC
test@test:~$ echo "wall 'HI IT幫'" | at now
warning: commands will be executed using /bin/sh
job 8 at Tue Oct 20 03:45:00 2020
                                                                               
Broadcast message from test@test (somewhere) (Tue Oct 20 03:45:45 2020):       
                                                                               
HI IT幫

一分鐘後發送全 users 訊息

date
echo "wall 'HI IT幫'" | at now + 1 minutes
test@test:~$ date
Tue 20 Oct 2020 03:49:43 AM UTC
test@test:~$ echo "wall 'HI IT幫'" | at now + 1 minutes
warning: commands will be executed using /bin/sh
job 10 at Tue Oct 20 03:50:00 2020
                                                                               
Broadcast message from test@test (somewhere) (Tue Oct 20 03:50:00 2020):       
                                                                               
HI IT幫

at -c jobid 查詢 at 內容

會幫忙 cd 轉到當初目錄 cd /home/test 挺智能的 :)

test@test:~$ at -c 5
#!/bin/sh
# atrun uid=1000 gid=1000
# mail test 0
umask 2
PWD=/home/test; export PWD
LOGNAME=test; export LOGNAME
XDG_SESSION_TYPE=tty; export XDG_SESSION_TYPE
MOTD_SHOWN=pam; export MOTD_SHOWN
HOME=/home/test; export HOME
LANG=en_US.UTF-8; export LANG
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=00: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:\*.dz=01\;31:\*.gz=01\;31:\*.lrz=01\;31:\*.lz=01\;31:\*.lzo=01\;31:\*.xz=01\;31:\*.zst=01\;31:\*.tzst=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:\*.wim=01\;31:\*.swm=01\;31:\*.dwm=01\;31:\*.esd=01\;31:\*.jpg=01\;35:\*.jpeg=01\;35:\*.mjpg=01\;35:\*.mjpeg=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:\*.ogv=01\;35:\*.ogx=01\;35:\*.aac=00\;36:\*.au=00\;36:\*.flac=00\;36:\*.m4a=00\;36:\*.mid=00\;36:\*.midi=00\;36:\*.mka=00\;36:\*.mp3=00\;36:\*.mpc=00\;36:\*.ogg=00\;36:\*.ra=00\;36:\*.wav=00\;36:\*.oga=00\;36:\*.opus=00\;36:\*.spx=00\;36:\*.xspf=00\;36:; export LS_COLORS
SSH_CONNECTION=172.26.80.1\ 55394\ 172.26.84.29\ 22; export SSH_CONNECTION
LESSCLOSE=/usr/bin/lesspipe\ %s\ %s; export LESSCLOSE
XDG_SESSION_CLASS=user; export XDG_SESSION_CLASS
LESSOPEN=\|\ /usr/bin/lesspipe\ %s; export LESSOPEN
USER=test; export USER
SHLVL=0; export SHLVL
XDG_SESSION_ID=669; export XDG_SESSION_ID
XDG_RUNTIME_DIR=/run/user/1000; export XDG_RUNTIME_DIR
SSH_CLIENT=172.26.80.1\ 55394\ 22; export SSH_CLIENT
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop; export XDG_DATA_DIRS
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin; export PATH
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus; export DBUS_SESSION_BUS_ADDRESS
SSH_TTY=/dev/pts/2; export SSH_TTY
cd /home/test || {
	 echo 'Execution directory inaccessible' >&2
	 exit 1
}
touch test.txt

atq 查詢目前還有多少 at 再跑

test@test:~$ atq
9	Tue Oct 20 11:47:00 2020 a test
5	Tue Oct 20 11:40:00 2020 a test

atrm jobid 刪除 at

test@test:~$ atrm 9
test@test:~$ atq
5	Tue Oct 20 11:40:00 2020 a test

測試發生問題

按 backspace 出現 ^H

xxx

調查 linux - Backspace, Tab not working in terminal (using ssh) - Unix & Linux Stack Exchange
要按 ctrl+v+backspace 才能刪減文字(好反直覺阿!!!)

備註 :

沒有啟用,測試用以下 script

test@test:~$ systemctl restart atd
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'atd.service'.
Authenticating as: test
Password: 
==== AUTHENTICATION COMPLETE ===


test@test:~$ systemctl enable atd
Synchronizing state of atd.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable atd
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ===
Authentication is required to reload the systemd state.
Authenticating as: test
Password: 
==== AUTHENTICATION COMPLETE ===
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ===
Authentication is required to reload the systemd state.
Authenticating as: test
Password: 
polkit-agent-helper-1: pam_authenticate failed: Authentication failure
==== AUTHENTICATION FAILED ===
Failed to reload daemon: Access denied
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-unit-files ===
Authentication is required to manage system service or unit files.
Authenticating as: test
Password: 
==== AUTHENTICATION COMPLETE ===
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ===
Authentication is required to reload the systemd state.
Authenticating as: test
Password: 
==== AUTHENTICATION COMPLETE ===

明天從 鳥哥私房菜 - 第十五章、例行性工作排程(crontab) batch:系統有空時才進行背景任務 開始~ ~ ~


上一篇
第46天-fd-find 代替 find
下一篇
第48天-學習 crontab 工作排程
系列文
30天菜鳥學 Linux 59

尚未有邦友留言

立即登入留言