bash + command 在 Linux shell 上正常工作,但在定时任务中却无法正常运行。

huangapple go评论67阅读模式
英文:

bash + command working fine on linux shell but not from cron job

问题

以下是您要翻译的部分:

"我正在尝试使用cron作业在我们的Linux机器上禁用交换分区。

首先,我想解释在Linux机器上禁用交换分区的正确方法(我们使用的是RHEL 7.x)。

应该按照以下步骤进行:

  1. swapoff -a &(在这个示例中,我们将其作为进程使用)

  2. /etc/fstab文件中注释掉包含交换分区的行,如:

  3. #/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.txt 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

huangapple
  • 本文由 发表于 2023年6月22日 14:00:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定