为crontab编写python条件 [英] Writing python conditionals for crontab

查看:131
本文介绍了为crontab编写python条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在crontab中编辑python if语句如何工作?使用python 3.6.3

How does a python if statement work when editing it in crontab? Using python 3.6.3

使用linux crontab,我试图获取目录(目录。和。以外)中隐藏文件的数量。 ),这样我就可以安排警报了。以下内容以字符串格式获取金额:

Using linux crontab I am trying to get a count of the amount of hidden files within a directory (other than the directories "." and "..") so I can schedule an alert. The following gets the amount in string format:

import subprocess
res = subprocess.run('ls -Ap | egrep "^\..*/$" | wc -l', shell=True, universal_newlines=True, stdout=subprocess.PIPE);
print(res.stdout)

output>>> (integer type string)

然后我想要类似的东西

if res.stdout != "0":
   (executeAlert.sh script)
else:
   (ignore)

我们将提供任何帮助。

推荐答案

一个更好的解决方案是完全避免子过程。 (即使您使用子流程,也不要使用 ls 在脚本中。)

A much better solution is to avoid a subprocess entirely. (And even if you use a subprocess, don't use ls in scripts.)

from pathlib import Path

dotfiles = Path().glob('.[!.]*')
if len(list(dotfiles)) > 0:
    do_things()

实际上,实际上并不需要检查长度;您可以说

Actually, there is no real need to check the length; you can say

dotfiles = list(Path().glob('.[!.]*'))
if dotfiles:
    do_things('The files which matched are %s' % dotfiles)

有点晦涩但更简洁的是,您可以遍历生成器。仅当至少有一个匹配的文件时,才会执行循环体。如果您只关心是否至少有一个文件,这是一种优化,因为一旦找到第一个文件,该文件就会停止;

Somewhat obscurely, but more succinctly, you could iterate over the generator instead; the loop body will be executed only if there is at least one file which matches. This is an optimization if you only care whether there is at least one file, as it stops once it finds the first one;

for file in Path().glob('.[!.]*'):
    do_things()
    break

Python不在乎是否从 cron 运行脚本(尽管有时您需要在 cron 作业(如果您在非标准位置安装了库)。但是,如果您所有的业务逻辑都在shell脚本中,为什么还要在这里使用Python? (尽管您的Shell脚本也可以通过研究一些避免使用的反模式来受益。)

Python doesn't care whether you run your script from cron or not (though sometimes you need to arrange the environment for Python in your cron job if you have libraries installed in nonstandard locations). But why are you using Python at all here if all your business logic is in shell scripts? (Though your shell scripting could also benefit from studying some antipatterns to avoid.)

这里完全是用类似的逻辑在shell脚本中完成的事情:

Here's the whole thing entirely in shell script, using similar logic:

for file in .[!.]*; do
    test -e "$file" || break  # cf nullgbob
    executeAlert.sh
    break
done

Bash优惠 shopt -s nullglob 以避免在通配符不匹配时进入循环;但是 cron 运行 sh ,在这里完全避免bashisms并不是很困难的(尽管在没有匹配项的情况下会出现通配行为) 令人惊讶)。

Bash offers shopt -s nullglob to avoid entering the loop if the wildcard has no match; but cron runs sh and it's not altogether hard to avoid bashisms here (though the globbing behavior in the case of no matches is surprising).

如果要在shell脚本中找到所有匹配项,可以说

If you want all the matches in a shell script, you can say

set -- .[!.]*
if [ "$@" != ".[!.]*" ]; then
    executeAlert.sh "$@"   # argument is list of matching files
fi

如果使用Bash或ksh,则可以将匹配项收集到数组;

If you use Bash or ksh, you could collect the matches into an array;

shopt -s nullglob
dotfiles=(.[!.]*)
for file in "${dotfiles[@]}"; do
   echo "Here's one dotfile: $file"
done
if [[ "${#dotfiles[@]}" > 0 ]]; then
     executeAlert.sh "${dotfiles[@]}"
fi

您的 egrep 正则表达式仅在点目录; 我以为这是一个错误。

Your egrep regex would only match on dot directories; I have assumed that this was a mistake.

这篇关于为crontab编写python条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆