简体   繁体   English

在Bash脚本中使用&内部脚本有不利之处吗?

[英]Are there downsides of using & inside Bash scripts?

I am making a script that should install MariaDB/MySQL server and then create users and such. 我正在制作一个脚本,该脚本应安装MariaDB / MySQL服务器,然后创建用户等。 For this I need mysqld_safe to be running, which doesn't have a daemon mode, so the script won't continue. 为此,我需要运行mysqld_safe ,它没有守护程序模式,因此脚本不会继续。 Doing mysqld_safe & works it seams. mysqld_safe &并接缝。

/usr/bin/mysqld_safe > /dev/null 2>&1 &

RET=1
while [[ RET -ne 0 ]]; do
    sleep 5
    mysql -uroot -e "status" > /dev/null 2>&1
    RET=$?
done

...

Question

Are there downsides, or is it bad practice, to use & inside bash scripts? 在bash脚本中使用&内部脚本是否有弊端或不好的做法?

If, what is the recommended method? 如果是,推荐的方法是什么?

You want a negative answer, but I do this also to improve a stability of my script in raspberry. 您想要一个否定的答案,但是我这样做也是为了提高我的脚本在树莓派中的稳定性。

I also store PID of the process so that I could kill it later in the script. 我还存储该进程的PID,以便稍后可以在脚本中将其杀死。

I use lockfile, and I check the existence of the 'mysql_safe' before running it. 我使用锁文件,并在运行前检查“ mysql_safe”的存在。

However, I cannot comment on if it is a good practice. 但是,我不能评论这是否是一种好习惯。

I don't think you have any alternative to & if you need to run several processes in parallel, and it looks like you really do. 如果您需要并行运行多个进程,我认为您没有&替代方法,而且看起来确实如此。

Running several parallel processes without good reason exposes you to all kinds of race conditions and therefore should be avoided, hence the general advice not to use & thoughtlessly in your scripts. 无正当理由运行多个并行的进程使你接触到各种比赛条件,因此应尽量避免,因此,一般建议不要使用&脚本中的轻率。

I just hope you didn't forget to terminate the mysqld_safe process at the end of your script. 我只是希望您不要忘记在脚本结尾处终止mysqld_safe进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM