# tail => 尾巴# head => 头
# 查看后5行
[root@stream9 ~]# cat 1.txt
1
2
...
19
20
[root@stream9 ~]#
[root@stream9 ~]# tail -5 1.txt
16
17
18
19
20
# 默认看后10行
[root@stream9 ~]# tail 1.txt
12
13
14
15
16
17
18
19
20
21
# 前3行
[root@stream9 ~]# head -3 1.txt
1
2
3
# 试试查看谁登录该虚拟机的日志
[root@stream9 log]# pwd
/var/log # 专门存放所有日志的目录
[root@stream9 log]# ls
anaconda chrony dnf.log hawkey.log lastlog private sa sssd wtmp
audit cron dnf.rpm.log journal maillog qemu-ga secure tallylog
btmp dnf.librepo.log firewalld kdump.log messages README spooler tuned
[root@stream9 log]# tail -f secure # 举例
Oct 16 16:05:04 stream9 su[4129]: pam_unix(su-l:session): session closed for user admin
Oct 16 17:52:53 stream9 sshd[4056]: pam_unix(sshd:session): session closed for user root
Oct 16 19:11:15 stream9 sshd[4319]: Failed password for root from 10.10.11.1 port 48430 ssh2
Oct 16 19:11:15 stream9 sshd[4319]: Failed password for root from 10.10.11.1 port 48430 ssh2
Oct 16 19:11:15 stream9 sshd[4319]: Connection closed by authenticating user root 10.10.11.1 port 48430 [preauth]
Oct 16 19:11:23 stream9 sshd[4321]: Accepted password for root from 10.10.11.1 port 33842 ssh2
Oct 16 19:11:23 stream9 systemd[4326]: pam_unix(systemd-user:session): session opened for user root(uid=0) by (uid=0)
Oct 16 19:11:23 stream9 sshd[4321]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
Oct 16 20:59:11 stream9 sshd[4550]: Accepted password for root from 10.10.11.1 port 40728 ssh2
Oct 16 20:59:11 stream9 sshd[4550]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
Oct 16 21:03:00 stream9 sshd[4335]: Received disconnect from 10.10.11.1 port 33842:11: disconnected by user
Oct 16 21:03:00 stream9 sshd[4335]: Disconnected from user root 10.10.11.1 port 33842
Oct 16 21:03:00 stream9 sshd[4321]: pam_unix(sshd:session): session closed for user root
Oct 16 21:03:18 stream9 sshd[4646]: Accepted password for root from 10.10.11.1 port 40300 ssh2
Oct 16 21:03:18 stream9 sshd[4646]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
Oct 16 21:03:54 stream9 sshd[4648]: Received disconnect from 10.10.11.1 port 40300:11: disconnected by user
Oct 16 21:03:54 stream9 sshd[4648]: Disconnected from user root 10.10.11.1 port 40300
Oct 16 21:03:54 stream9 sshd[4646]: pam_unix(sshd:session): session closed for user root
2.crontab 定时执行任务
# 1. 查看帮助
[root@stream9 ~]# crontab --help
crontab: invalid option -- '-'
crontab: usage error: unrecognized option
Usage:
crontab [options] file
crontab [options]
crontab -n [hostname]
Options:
-u <user> define user
-e edit user's crontab
-l list user's crontab
-r delete user's crontab
-i prompt before deleting
-n <host> set host in cluster to run users' crontabs
-c get host in cluster to run users crontabs
-T <file> test a crontab file syntax
-s selinux context
-V print version and exit
-x <mask> enable debugging
Default operation is replace, per 1003.2
# 2. 添加定时任务 每隔一分钟执行脚本writeTime.sh
chmod +x writeTime.sh # 给该脚本执行权限
[root@stream9 ~]# crontab -e -u root
[root@stream9 ~]# crontab -l -u root
*/1 * * * * /root/writeTime.sh
[root@stream9 ~]#
[root@stream9 ~]# crontab -l
*/1 * * * * /root/writeTime.sh
# 3. 查看定时任务执行的结果
[root@stream9 ~]# cat writeTime.sh
#!/bin/bash
date +%Y-%m-%d_%H_%M_%S >> time.log
[root@stream9 ~]#
[root@stream9 ~]#
[root@stream9 ~]# cat time.log
2023-10-16_21_31_01
2023-10-16_21_32_02
2023-10-16_21_34_01
2023-10-16_21_35_01
2023-10-16_21_36_01
2023-10-16_21_37_01
2023-10-16_21_38_01
2023-10-16_21_39_01
# 3. 删除定时任务
[root@stream9 ~]# crontab -r -u root
[root@stream9 ~]#
[root@stream9 ~]# crontab -l
no crontab for root
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律