英文:
bash + command working fine on linux shell but not from cron job
问题
以下是您要翻译的部分:
"我正在尝试使用cron作业在我们的Linux机器上禁用交换分区。
首先,我想解释在Linux机器上禁用交换分区的正确方法(我们使用的是RHEL 7.x)。
应该按照以下步骤进行:
-
swapoff -a &
(在这个示例中,我们将其作为进程使用) -
在
/etc/fstab
文件中注释掉包含交换分区的行,如: -
#/dev/mapper/vg_lab_swap swap swap defaults 0 0
我们创建了名为swap_disable.cron
的cron作业。
cron作业如下所示(我们将cron作业设置为每分钟运行,仅用于测试):
* * * * * root [[ `swapon -s | wc | awk '{print $1}'` -ne 0 ]] && [[ `ps -ef | grep "swapoff -a" | grep -v grep | wc | awk '{print $1}'` -eq 0 ]] && (swapoff -a &) && sed -ie '/\/dev\/mapper\/vg_lab_swap/ s/^#*/#/' /etc/fstab
所以等待了一分钟以上后,我们发现/etc/fstab
文件没有更新,实际上交换分区也没有禁用。
因此,我们只在cron作业中设置了以下行,以了解我们的语法是否有问题:
更多的swap_disable.cron
:
* * * * * root [[ `swapon -s | wc | awk '{print $1}'` -ne 0 ]] && [[ `ps -ef | grep "swapoff -a" | grep -v grep | wc | awk '{print $1}'` -eq 0 ]] && echo why_line_not_works >/tmp/file.txt
解释:
-
swapon -s | wc | awk '{print $1}'
- 验证交换分区是否已禁用 -
ps -ef | grep "swapoff -a" | grep -v grep | wc | awk '{print $1}'
- 验证是否已经运行了swapoff进程
不幸的是,上面这个简单的行在cron作业中也不起作用,并且文件/tmp/file.txt
没有被创建。
那么我的cron作业有什么问题呢?
为什么这一行:
[[ `swapon -s | wc | awk '{print $1}'` -ne 0 ]] && [[ `ps -ef | grep "swapoff -a" | grep -v grep | wc | awk '{print $1}'` -eq 0 ]] && echo why_line_not_works >/tmp/file.txt
在Linux shell控制台中正常工作,但在cron作业中不起作用呢?
例如,当我们从sh运行它时:
cron.d]# sh
sh-4.2# [ `swapon -s | wc | awk '{print $1}'` -ne 0 ] && [ `ps -ef | grep "swapoff -a" | grep -v grep | wc | awk '{print $1}'` -eq 0 ] && echo why
why
英文:
I am trying to disable the swap on our Linux machines with cron job
first I want to explain the right approach for disabling swap on linux machines ( we have RHEL 7.x )
the process should be like this
1 swapoff -a &
( in this example we used it as process )
2 comment the line with swap in /etc/fstab
as:
3 #/dev/mapper/vg_lab_swap swap swap defaults 0 0
we create cron job with name - swap_disable.cron
and cron jon looks like this ( we set the cron job for every min just for testing )
* * * * * root [[ ` swapon -s | wc | awk '{print $1}' ` -ne 0 ]] && [[ ` ps -ef | grep "swapoff -a" | grep -v grep | wc | awk '{print $1}' ` -eq 0 ]] && ( swapoff -a & ) && sed -ie '/\/dev\/mapper\/vg_lab_swap/ s/^#*/#/' /etc/fstab
so after waiting more then one min we saw that /etc/fstab
file not update and actually swap not disabled
so we set only the following line in the cron job to understand if something with our syntax is wrong
more swap_disable.cron
* * * * * root [[ ` swapon -s | wc | awk '{print $1}' ` -ne 0 ]] && [[ ` ps -ef | grep "swapoff -a" | grep -v grep | wc | awk '{print $1}' ` -eq 0 ]] && echo why_line_not_works >/tmp/file.txt
explain:
swapon -s | wc | awk '{print $1}'
- verify if swap is disabled
ps -ef | grep "swapoff -a" | grep -v grep | wc | awk '{print $1}'
- verify if swapoff process already running
and unfortunately this above simple line also not works , and file /tmp/file.tx
t not created
so what is wrong in my cron job?
why the line:
[[ ` swapon -s | wc | awk '{print $1}' ` -ne 0 ]] && [[ ` ps -ef | grep "swapoff -a" | grep -v grep | wc | awk '{print $1}' ` -eq 0 ]] && echo why_line_not_works >/tmp/file.txt
is working fine on linux shell console but not from cron job?
example when we run it from sh
cron.d]# sh
sh-4.2# [ ` swapon -s | wc | awk '{print $1}' ` -ne 0 ] && [ ` ps -ef | grep "swapoff -a" | grep -v grep | wc | awk '{print $1}' ` -eq 0 ] && echo why
why
答案1
得分: 1
确保在您的crontab中声明SHELL和PATH,以便不必为运行的所有命令指定绝对路径。
尝试在您的cron.d文件中使用以下内容:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * root [[ `swapon -s | wc | awk '{print $1}'` -ne 0 ]] && [[ `ps -ef | grep "swapoff -a" | grep -v grep | wc | awk '{print $1}'` -eq 0 ]] && echo why_line_not_works >/tmp/file.txt
英文:
Make sure you declare your SHELL and PATH in your crontab to not have to specify absolute paths for all the commands you are running.
Try the following under your cron.d file:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * root [[ ` swapon -s | wc | awk '{print $1}' ` -ne 0 ]] && [[ ` ps -ef | grep "swapoff -a" | grep -v grep | wc | awk '{print $1}' ` -eq 0 ]] && echo why_line_not_works >/tmp/file.txt
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论