简体   繁体   中英

Run bash script after Apache2 restarts on Ubuntu

I use Ubuntu 16.04.

I want to add a hook that is invoked once Apache2 restarts and runs a command line (bash).

For example

php artisan queue:work --queue=high

Ideas?

While not ideal as it is in no way a "hook"... you could write a script to check the status of the apache program and toggle a "switch" variable.

#!/bin/bash

s=0; 
while true; do
    /etc/init.d/apache2 status > /dev/null;

    if [ $? -eq "0" ] && [ ! "${s}" -eq $? ]; then 
        # Do whatever you want to do when apache first starts running.
    fi

    s=$?
done

So we have a s witch. We check the return code of status on apache2. 0 Appeared to be when its running, not 0 (3) if it wasn't running.

$? returns the status code of the last run command if you didn't know.

if [ $? -eq "0" ] && [ ! "${s}" -eq $? ]; If apache is running, but it wasn't running the last time we checked, it just started! Let's do some work.

s=$? This line sets the switch to the last status code... means next loop $s will actually eq $? so the loop won't run.

I highly suggest you add in a sleep 1 or whatever an acceptable delay is between apache starting and your program running is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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