webhookd icon indicating copy to clipboard operation
webhookd copied to clipboard

execute bash script in background

Open urbantrout opened this issue 5 years ago • 3 comments
trafficstars

Hi @ncarlier,

thanks for this nice piece of software.

I want to execute a bash script as a background job, so that the HTTP request exits immediatly. I wasn't able to do this with

nohup ./do-stuff.sh >/dev/null 2>&1 &

Is there a way to solve this?

urbantrout avatar Aug 13 '20 13:08 urbantrout

Hi @urbantrout,

I think the pb is due to the fact that ./do-stuff.sh is relative and therefore not properly found. The working directory is where you start webhookd. You can add pwd in your script to check that. Can you replace /dev/null by something like /tmp/output.log in order to have a clear error message?

ncarlier avatar Aug 15 '20 07:08 ncarlier

I had the same issue. Then I used screen to run the script in background screen -dm -S screen1 /bin/bash 'do-stuff.sh'

ngocsanguit avatar Apr 27 '21 14:04 ngocsanguit

Hi, screen is an option but you should be able to do the same thing with a simple nohup.

Here a simple working example:

Let's create a script that simulate a long job: scripts/long.sh

#!/bin/bash

echo "Starting long script..."
for i in {1..20}; do
  sleep 1
  echo "running ${i} ..."
done
echo "Long script end"
exit 0

If we call the webhook (http://localhost:8080/long) with the default configuration the script is killed because exceeding 10 sec. But if we create another script: scripts/bg.sh

#!/bin/bash

echo "Starting background job..."
nohup ./scripts/long.sh >/tmp/debug.log 2>&1  &
echo "Background job started."

When we call the webhook (http://localhost:8080/bg) we have the fast following response:

data: Starting background job...
data: Background job started.

And the /tmp/debug.log file continues to be fed as expected. So I don't understand your issue. Can you provide a non-working example?

ncarlier avatar Apr 27 '21 19:04 ncarlier