如何检查文件是否存在并执行命令(如果不存在)? [英] How can I check a file exists and execute a command if not?

查看:198
本文介绍了如何检查文件是否存在并执行命令(如果不存在)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Python编写的守护程序.运行时,它的PID文件位于/tmp/filename.pid.如果守护程序未运行,则PID文件不存在.

I have a daemon I have written using Python. When it is running, it has a PID file located at /tmp/filename.pid. If the daemon isn't running then PID file doesn't exist.

在Linux上,如何检查以确保PID文件存在,如果不存在,请执行命令将其重新启动?

On Linux, how can I check to ensure that the PID file exists and if not, execute a command to restart it?

命令应该是

python daemon.py restart

必须从特定目录执行.

推荐答案

[ -f /tmp/filename.pid ] || python daemon.py restart

-f检查给定路径是否存在并且是常规文件(只是-e检查路径是否存在)

-f checks if the given path exists and is a regular file (just -e checks if the path exists)

[]执行测试,并在成功后返回0,否则返回1

the [] perform the test and returns 0 on success, 1 otherwise

||是类似于C的or,因此,如果左侧的命令失败,请执行右侧的命令.

the || is a C-like or, so if the command on the left fails, execute the command on the right.

最后一条语句说,如果/tmp/filename.pid不存在,则启动守护程序.

So the final statement says, if /tmp/filename.pid does NOT exist then start the daemon.

这篇关于如何检查文件是否存在并执行命令(如果不存在)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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