内容一:只显示最近一次开机时间
1.1 使用 top 命令
# top -bn1 | head -1
1.2 使用 who 命令
# who -b
1.3 使用 uptime 命令
# uptime
内容二:显示最近多次开机时间
# last reboot
# top -bn1 | head -1
# who -b
# uptime
# last reboot
# TMOUT=900
或者:
# export TMOUT=0
(
补充:
1) 这里以设置超过 900 秒用户就会超时为例
2) 如果 TMOUT=0 则用户永不会超时
)
# vim /etc/profile
添加以下内容:
......
TMOUT=900
(
补充:
1) 这里以设置超过 900 秒用户就会超时为例
2) 如果 TMOUT=0 则用户永不会超时
3) 补充 /etc/bashrc 文件会比 /etc/profile 文件更有优先级
)
# source /etc/profile
# vim /etc/bashrc
添加以下内容:
......
TMOUT=900
(
补充:
1) 这里以设置超过 900 秒用户就会超时为例
2) 如果 TMOUT=0 则用户永不会超时
3) 补充 /etc/bashrc 文件会比 /etc/profile 文件更有优先级
)
# source /etc/bashrc
# echo $TMOUT
当通过以上设置造成用户登录超时时,系统会输出以下内容:
# timed out waiting for input: auto-logout
# vim /root/12456.sh
创建以下内容:
#!/bin/bash
for i in {1..5}
do
echo $i
done
(补充:这里以创建 /etc/root/for.sh 脚本为例)
# vim /etc/systemd/system/12456.service
创建以下内容:
[Unit]
Description=12345
After=default.target
[Service]
Type=oneshot
ExecStart=/root/12456.sh
[Install]
WantedBy=default.target
(补充:这里以创建 /etc/systemd/system/12456.service 来管理 ExecStart=/root/12456.sh 为例)
# systemctl daemon-reload
# chmod u+x /etc/systemd/system/12456.service
# systemctl start 12456.service
# systemctl stop 12456.service
# systemctl restart 12456.service
# systemctl enable 12456.service
# crontab -e
# crontab -u root -e
(补充:这里以使用 root 用户的身份进入周期任务计划的文件为例)
<what minute> <what hour> <day of the month> <what month> <day of the week> <command>
(
补充:
1) 当以上内容是 1 个星号 “*” 时,代表任意时刻都会执行
2) 当在某一个时刻执行时,可以直接写入对应的数字,例如:0,代表在 0 时执行
3) 当同时在多个时刻执行时,可以直接写入多个对应的数字,例如:0,15,30,代表在 0 时、15 时、30 时都会执行
4) 当每隔一段时间就执行时,可以在星号 “*” 后面添加斜杠和间隔的数字,例如:*/5,代表每隔 5 就会执行 1 次
5) 当是执行 /sbin 下的命令时,需要使用命令的全路径
6) 有些特殊符号不会在此地起作用,例如:$(<command>)
)
0 1 * * * curl eternalcenter.com
(补充:此案例会在每天 1 点 0 时访问一次 eternalcenter.com)
*/5 * * * * curl eternalcenter.com
(补充:此案例会每过 5 分钟访问一次 eternalcenter.com)
0 0 1,5,10,15 /sbin/reboot
(补充:此案例会在每个月的 1 号 5 号 10 号 15 号的 0 点 0 分重启系统)
# crontab -l
# crontab -u root -l
(补充:这里以显示 root 用户已设置的周期任务计划为例)
如果是 Rocky Linux & RHEL
/var/spool/cron/
如果是 openSUSE & SLES
/var/spool/cron/tabs/
通过系统日志显示程序被关闭的原因
# vim /var/log/message
或者:
# journalctl
或者:
# dmesg –T
(补充:这里的 -T 代表要显示时间)