webhookd
webhookd copied to clipboard
execute bash script in background
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?
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?
I had the same issue. Then I used screen to run the script in background
screen -dm -S screen1 /bin/bash 'do-stuff.sh'
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?