20180720_jeffrey - silenceuncrio/diary GitHub Wiki
加一下 at
command
recipe 就叫作 at
diff --git a/meta-proscend/recipes-core/prosrc/prosrc_0.1.bb b/meta-proscend/recipes-core/prosrc/prosrc_0.1.bb
index a6bfccc..4816402 100644
--- a/meta-proscend/recipes-core/prosrc/prosrc_0.1.bb
+++ b/meta-proscend/recipes-core/prosrc/prosrc_0.1.bb
@@ -62,7 +62,7 @@ RDEPENDS_${PN} += " syslog-ng"
RDEPENDS_${PN} += " libcgroup"
# Schedule Reboot
-RDEPENDS_${PN} += " cronie"
+RDEPENDS_${PN} += " cronie at"
# ======================================
不過先確認 crontab 夠不夠用
先用 /etc/cron.d/test
來測試 crond 是怎麼作到每 40 分鐘
# test per 40 minutes
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
*/40 * * * * root date >> /tmp/test.txt
記得重啟 crond
root@Cellular Router:~# /etc/init.d/crond restart
Stopping crond: OK
Starting crond: OK
看一下 /tmp/test.txt
root@Cellular Router:~# cat /tmp/test.txt
Fri Jul 20 03:40:01 UTC 2018
Fri Jul 20 04:00:02 UTC 2018
Fri Jul 20 04:40:01 UTC 2018
Fri Jul 20 05:00:01 UTC 2018
這根本不叫每 40 分鐘
玩一下 at
從現在開始的五分鐘後
利用 date
輸出時間然後寫到 /tmp/test_at.txt
去
root@Cellular Router:~# at now + 5 minutes
warning: commands will be executed using /bin/sh
at> date > /tmp/test_at.txt
at> <EOT>
job 2 at Fri Jul 20 05:15:00 2018
at now + 5 minutes
輸入後按 enter
會進入 at
與您的互動模式
輸入 /tmp/test_at.txt
完成後按 ctrl + d
會出現 <EOF>
的字樣
代表結束
將上述的第 2 項工作內容列出來查閱
root@Cellular Router:~# at -c 2
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
umask 22
HUSHLOGIN=FALSE; export HUSHLOGIN
USER=root; export USER
MAIL=/var/spool/mail/root; export MAIL
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/sbin/icos; export PATH
PWD=/home/root; export PWD
TZ=GREENWI+00:00; export TZ
PS1=\\u@\\h:\\w\\\$\ ; export PS1
SHLVL=1; export SHLVL
HOME=/home/root; export HOME
LOGNAME=root; export LOGNAME
OLDPWD=/etc/cron.d; export OLDPWD
cd /home/root || {
echo 'Execution directory inaccessible' >&2
exit 1
}
date > /tmp/test_at.txt
利用 atq
查詢目前主機上面有多少的 at
工作排程
root@Cellular Router:~# atq
2 Fri Jul 20 05:15:00 2018 a root
等吧
再利用 atq
查詢目前主機上面有多少的 at
工作排程
root@Cellular Router:~# atq
root@Cellular Router:~#
觀察 /tmp/test_at.txt
root@Cellular Router:~# cat /tmp/test_at.txt
Fri Jul 20 05:15:01 GREENWI 2018
如果是 100 分鐘的話呢
看一下目前時間
root@Cellular Router:~# date
Fri Jul 20 05:24:21 GREENWI 2018
看來是可以
root@Cellular Router:~# at now + 100 minutes
warning: commands will be executed using /bin/sh
at> date >> /tmp/test_at.txt
at> <EOT>
job 3 at Fri Jul 20 07:04:00 2018
研究一下怎麼跳過 at
的互動模式
之前有利用 sfdisk
分割過 flash
該 shell script 如下
#!/bin/sh
...
sfdisk --force ${node} << EOF
${BOOT_ROM_SIZE}M,80M,C
100M,400M,L
500M,400M,L
900M,,L
EOF
準備一隻 script 試一下 - /tmp/test_at.sh
#!/bin/sh
at now + 10 minutes << EOF
date >> /tmp/test_at.txt
EOF
成功了
root@Cellular Router:~# date
Fri Jul 20 05:32:41 GREENWI 2018
root@Cellular Router:~# /tmp/test_at.sh
warning: commands will be executed using /bin/sh
job 4 at Fri Jul 20 05:42:00 2018
再來讓 /tmp/test_at.sh
能接受參數就能從 icos module 依據設定的分鐘數來使用 at
了
另一個要克服的是寫 log 的問題
要不然系統是為了什麼而 reboot 的就沒人知道了
利用下面的 command 即可
logger -t "<<group_name:module_name>>" "hello world"
再寫一隻 script 試試 - /tmp/test_reboot.sh
#!/bin/sh
if [ $# -lt 1 ]; then
echo " Usage: $0 (minutes)"
exit 1
fi
at now + $1 minutes << EOF
date >> /tmp/test_reboot.txt
logger -t "<<MISC:SCHEDULER>>" "reboot"
EOF
exit 0
試試吧
root@Cellular Router:~# /tmp/test_reboot.sh
Usage: /tmp/test_reboot.sh (minutes)
root@Cellular Router:~# /tmp/test_reboot.sh 3
warning: commands will be executed using /bin/sh
job 5 at Fri Jul 20 06:31:00 2018
root@Cellular Router:~# date
Fri Jul 20 06:28:15 GREENWI 2018
搭配 at
的 recipe - ./poky/meta/recipes-extended/at/at_3.1.16.bb
跟 cronie 一樣
我需要 BitBake append file 來修正原來的 at recipe
meta-proscend/recipes-extended/at/at_3.1.16.bbappend
目的是為了擺脫 SysVinit 在一開機就幫我們啟動 at service
確認一下 crontab 寫 log 的事宜
root@Cellular Router:~# cat /etc/cron.d/test
# test per 40 minutes
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
*/2 * * * * root date >> /tmp/test.txt; logger -t "<<MISC:SCHEDULER>>" "reboot"
記得 restart crond
root@Cellular Router:~# /etc/init.d/crond restart
Stopping crond: OK
Starting crond: OK
等等就可以直接從 web ui 來確認 log
root@Cellular Router:~# tail /home/log/slogd.log
...
2018-07-20 06:47:43 Cellular Router daemon.info <<HARDWARE:LTE>>: (3) Singal level is too Low(2) Time Out!
2018-07-20 06:48:01 Cellular Router cron.info CROND[5437]: (root) CMD (date >> /tmp/test.txt; logger -t "<<MISC:SCHEDULER>>" "reboot")
2018-07-20 06:48:01 Cellular Router user.notice <<MISC:SCHEDULER>>: reboot
...
尷尬的是 crond 自己也會去寫 log
可能要讓 crond 去執行一隻 script
再讓該 script 作 寫 log 和 reboot 的事宜
/etc/cron.d/schedule_reboot
# test per 40 minutes
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
*/2 * * * * root /tmp/schedule_reboot.sh
/tmp/schedule_reboot.sh
#!/bin/sh
date >> /tmp/schedule_reboot.txt
logger -t "<<MISC:SCHEDULER>>" "reboot"
exit 0
搞定
再寫一隻 /tmp/at_reboot.sh
#!/bin/sh
if [ $# -lt 1 ]; then
echo " Usage: $0 (minutes)"
exit 1
fi
at now + $1 minutes << EOF
/tmp/schedule_reboot.sh
EOF
exit 0
試試吧
root@Cellular Router:~# /tmp/at_reboot.sh 2
warning: commands will be executed using /bin/sh
job 6 at Fri Jul 20 07:05:00 2018
搞定
可以調整 schedule_reboot_t
了
先確認 meta-proscend/recipes-extended/at/at_3.1.16.bbappend
的有效性
目前在 /etc/rc0.d/
會看到 K20atd
root@Cellular Router:~# ls -al /etc/rc0.d/
drwxr-xr-x 2 root root 864 Jan 1 1970 .
drwxr-xr-x 40 root root 6792 Jul 20 01:24 ..
lrwxrwxrwx 1 root root 13 Jan 1 1970 K20atd -> ../init.d/atd
這會讓 /etc/init.d/atd
在一開機就被執行
確認無誤
root@Cellular Router:~# /etc/init.d/atd status
/usr/sbin/atd is stopped
root@Cellular Router:~# /etc/init.d/crond status
crond is stopped
先上 code
commit 59187b963dbe7bd29ff391e562568af4f8dff2cf
Refs: [develop], {origin/develop}
Author: jeffrey <[email protected]>
Date: Fri Jul 20 15:16:48 2018 +0800
add 'at' recipe for 'schedule reboot':
- Use .bbappend to cause update-rc.d to disable init script start links of at service
meta-proscend/recipes-core/prosrc/prosrc_0.1.bb | 2 +-
meta-proscend/recipes-extended/at/at_3.1.16.bbappend | 4 ++++
2 files changed, 5 insertions(+), 1 deletion(-)
利用 at
在五分鐘後重開機
不過重開後發現 atd
叫不起來
紀錄一下 console log
root@Cellular Router:~# at now + 2 minutes
warning: commands will be executed using /bin/sh
at> reboot
at> <EOT>
job 1 at Fri Jul 20 09:45:00 2018
Can't open /var/run/atd.pid to signal atd. No atd running?
root@Cellular Router:~# /etc/init.d/atd start
Starting atd: OK
root@Cellular Router:~# at now + 2 minutes
warning: commands will be executed using /bin/sh
at> reboot
at> <EOT>
job 2 at Fri Jul 20 09:45:00 2018
root@Cellular Router:~# date
Fri Jul 20 09:43:45 GREENWI 2018
root@Cellular Router:~# atq
2 Fri Jul 20 09:45:00 2018 a root
1 Fri Jul 20 09:45:00 2018 a root
root@Cellular Router:~#
Broadcast message from root@Cellular Router (Fri Jul 20 09:45:01 2018):
The system is going down for reboot NOW!
...
Cellular Router login: root
Password:
root@Cellular Router:~# /etc/init.d/atd status
/usr/sbin/atd is stopped
root@Cellular Router:~# /etc/init.d/atd start
Starting atd: OK
root@Cellular Router:~# /etc/init.d/atd status
/usr/sbin/atd is stopped
root@Cellular Router:~# at now + 2 minutes
warning: commands will be executed using /bin/sh
Cannot open lockfile /var/spool/at/jobs/.SEQ: No such file or directory