Ubuntu系Linux定时任务

背景

最近公司天天统计谁下班不关机,被抓住就罚钱,甚是恶心,于是就在我的Linux mint整个定时任务,每天23点禁用网卡,早晨7点开启。因为他们统计的方式无非就是在某个时间点去ping 你的ip,禁用网卡我看你怎么ping。

开整

crontab分为普通权限和root权限,就是说如果你的定时任务需要su权限,那你可以这样:

sudo crontab -e #该命令下编辑,脚本会自动用su去执行
### 定时任务(命令前要加sudo)
0 23 * * * sudo ifconfig enp2s0 down
##每天七点开启网卡
0 7 * * * sudo ifconfig enp2s0 up

enp2s0 代表你的网卡名字,使用ifconfig可以看到,down:关闭网卡,up:开启网卡

$ ifconfig
enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.92.67  netmask 255.255.254.0  broadcast 192.168.93.255
        inet6 fe80::f5ad:8dde:9ffc:d489  prefixlen 64  scopeid 0x20<link>
        ether 48:4d:7e:b0:d8:0b  txqueuelen 1000  (Ethernet)
        RX packets 10792283  bytes 2714063395 (2.7 GB)
        RX errors 0  dropped 14565  overruns 0  frame 0
        TX packets 3259007  bytes 2859745325 (2.8 GB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 130074  bytes 30532449 (30.5 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 130074  bytes 30532449 (30.5 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

补充

如果你的定时脚本不需要su权限,可以直接使用crontab -e 无需加sudo

猜你喜欢

转载自blog.csdn.net/chen462488588/article/details/112259595
今日推荐