如何设置shell脚本的进程名称? [英] How to set the process name of a shell script?

查看:193
本文介绍了如何设置shell脚本的进程名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法设置shell脚本的进程名?这是使用 killall 命令终止此脚本所必需的.

Is there any way to set the process name of a shell script? This is needed for killing this script with the killall command.

推荐答案

这是一种方法,它是一种黑客/解决方法,但效果很好.随意调整它以满足您的需要,它当然需要对符号链接创建或使用 tmp 文件夹进行一些检查以避免可能的竞争条件(如果它们在您的情况下有问题).

Here's a way to do it, it is a hack/workaround but it works pretty good. Feel free to tweak it to your needs, it certainly needs some checks on the symbolic link creation or using a tmp folder to avoid possible race conditions (if they are problematic in your case).

包装器

#!/bin/bash
script="./dummy"
newname="./killme"

rm -iv "$newname"

ln -s "$script" "$newname"

exec "$newname" "$@"

虚拟

#!/bin/bash
echo "I am $0"
echo "my params: $@"

ps aux | grep bash

echo "sleeping 10s... Kill me!"
sleep 10

使用以下方法进行测试:

Test it using:

chmod +x dummy wrapper
./wrapper some params

在另一个终端中,使用:

In another terminal, kill it using:

killall killme

<小时>

注意事项

确保您可以写入当前文件夹(当前工作目录).


Notes

Make sure you can write in your current folder (current working directory).

如果您当前的命令是:

/path/to/file -q --params somefile1 somefile2

wrapper 中的 script 变量设置为 /path/to/file(而不是./dummy)并像这样调用wrapper:

Set the script variable in wrapper to /path/to/file (instead of ./dummy) and call wrapper like this:

./wrapper -q --params somefile1 somefile2

这篇关于如何设置shell脚本的进程名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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