Linux 获取shell脚本的pid并将其保存到锁定文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19477444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the pid of the shell script and save it into a lockfile
提问by user2693017
I do my backups with rsnapshot which creates a lockfile with the process pid inside. Now I would like to make a backup from the rsnapshots backup, so I′m looking for a way to create this lockfile for the second/external backup.
我使用 rsnapshot 进行备份,它创建了一个包含进程 pid 的锁文件。现在我想从 rsnapshots 备份做一个备份,所以我正在寻找一种方法来为第二个/外部备份创建这个锁文件。
The shell script should like this:
shell 脚本应该是这样的:
- check if a lockfile exists, if yes wait and try again(i′m doing this with a while true loop)
- get the pid of this shell script and save it as a rsnapshot lockfile
- start the second/external backup
- delete the lockfile
- 检查锁文件是否存在,如果是,请等待并重试(我正在使用 while true 循环执行此操作)
- 获取此 shell 脚本的 pid 并将其保存为 rsnapshot 锁定文件
- 启动第二个/外部备份
- 删除锁文件
How can I get the PID and save it as a rsnapshot lockfile ?
如何获取 PID 并将其保存为 rsnapshot 锁定文件?
采纳答案by Aleks-Daniel Jakimenko-A.
The PID is stored in $$
PID存储在 $$
Like
喜欢
echo $$ > thisscriptpidfile
回答by Bee Kay
For any application, you can find its Process ID using the Unix shell itself, using ps. Example below is a very reduced list from ps. PS will show you not only the PID, but also the owner, as well as the Parent Process ID (as in which process started this particular process.)
对于任何应用程序,您都可以使用 Unix shell 本身找到它的进程 ID,使用 ps。下面的示例是来自 ps 的非常简化的列表。PS 不仅会显示 PID,还会显示所有者以及父进程 ID(如哪个进程启动了此特定进程。)
userX# ps -ef | more
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Oct19 ? 00:00:00 /sbin/init
root 2 0 0 Oct19 ? 00:00:00 [kthreadd]
root 3 2 0 Oct19 ? 00:00:02 [migration/0]
root 4 2 0 Oct19 ? 00:04:48 [ksoftirqd/0]
root 5 2 0 Oct19 ? 00:00:00 [migration/0]
root 6 2 0 Oct19 ? 00:00:00 [watchdog/0]
...
root 27 2 0 Oct19 ? 00:00:00 [pm]
root 28 2 0 Oct19 ? 00:00:00 rsnapshot
root 29 2 0 Oct19 ? 00:00:00 [xenbus]
Now let's start finding which Process is interesting to us. I am not familiar with rsnapshot, so I've put dummy data in the examples.
现在让我们开始寻找我们感兴趣的进程。我不熟悉 rsnapshot,所以我在示例中放入了虚拟数据。
userX# ps -ef | grep rsnapshot
root 28 2 0 Oct19 ? 00:00:00 rsnapshot
ec2-user 7233 1497 0 11:32 pts/0 00:00:00 grep rsnapshot
Note that it does not give you the "header" information, only matching lines, thanks to grep. Your second "column" is the PID. Noteworthy: ps shows everyprocess, including the grep you just ran. Your commands/scripts need to be wary of this and strip out these items. I will use awk in the next example to do just that.
请注意,由于 grep,它不会为您提供“标题”信息,仅提供匹配的行。您的第二个“列”是 PID。值得注意: ps 显示每个进程,包括您刚刚运行的 grep。您的命令/脚本需要对此保持警惕并删除这些项目。我将在下一个示例中使用 awk 来做到这一点。
And now to expand further, getting the PID into a file. We need to confirm we have a PID, and if so, create the command to create the lock file:
现在进一步扩展,将 PID 放入一个文件中。我们需要确认我们有一个 PID,如果有,创建命令来创建锁定文件:
userX# ps -ef | grep rsnapshot | awk 'userX# ps -ef | grep rsnapshot | awk '##代码##!~/grep/ && ~/[0-9]/{print "echo "" > rsnapshot.lck"}' | sh
!~/grep/ && ~/[0-9]/{print "echo "" > rsnapshot.lck"}'
echo 28 > rsnapshot.lck
If no PID for rsnapshot exists, then there will be no output. As writtent, awk will review each line, and if it does not contain the string "grep" AND there is any digit [0-9] in the second field, then print the command to be run - but not actually run the command.
如果 rsnapshot 的 PID 不存在,则不会有输出。正如所写的那样,awk 将检查每一行,如果它不包含字符串“grep”并且第二个字段中有任何数字 [0-9],则打印要运行的命令 - 但不实际运行该命令。
The final step is to invoke the command, from the awk output.
最后一步是从 awk 输出调用命令。
##代码##Adding "| sh" causes all output to be invoked as a command. If awk does not find rsnapshot, then there is no command to run.
添加“| sh”会导致所有输出作为命令调用。如果 awk 没有找到 rsnapshot,则没有命令可以运行。