search

Home  >  Q&A  >  body text

How to stop background process from running shell script

<p>I'm trying to run mysqld in the background to execute some commands using the mysql client, but when I switch the process to the foreground, my script stops running. This is the script I use: </p> <pre class="brush:sh;toolbar:false;">set -m daemon="/usr/bin/mysqld --user=mysql --console --skip-name-resolve --skip-networking=0 $@" $daemon & exec echo "Test" exec fg %1 </pre> <p>I am not a shell script programming expert, I looked on some websites and found this method of executing commands. </p>
P粉722409996P粉722409996491 days ago728

reply all(1)I'll reply

  • P粉521748211

    P粉5217482112023-09-04 17:45:03

    No. The script stops after exec echo "Test". exec Replace the current process (i.e. shell script) with the following command. You can verify by:

    daemon="/usr/bin/mysqld --user=mysql --console --skip-name-resolve --skip-networking=0 $@"
    $daemon &
    exec echo "Test"
    echo "Second Test"
    exec fg %1

    The solution is simple: don't use exec.

    BTW: It's not clear why you want to use fg in a shell script. In an interactive shell, fg has no function only within scripts.

    reply
    0
  • Cancelreply