I have a script that looks like this:
#!/bin/bash
tsx watch services/$1/src/index.ts
This will start tsc
for the given service:
script/start_service some-service
If I add &
the end it gets executed as a job:
script/start_service some-service &
but I still get some output. Then if I redirect (all) its output to /dev/null
:
script/start_service some-service > /dev/null 2>&1 &
it starts the service properly in the background, but I can't look it up after my script returns.
What I'm trying to figure out is how to add a name (or some other unique identifier) to the job I start so that I can look it up later from another script (and possibly terminate it if necessary).
Note that this question is related, but I can't figure out how to apply the answer to my use case. The answer:
bash -c "exec -a MyUniqueProcessName <command> &"
doesn't work for me. No matter what I do I don't get a process with the name I passed.
Note that the reason why I don't just assign the pid
to a variable is that my script terminates and whenever the user calls my script again it needs to check which services are running at the moment and it doesn't have access to the ephemeral local variables that would have been created by the previous execution of the script.
screen
to start the script in the background. – user68186 Mar 20 '24 at 19:00screen
work? – Adam Arold Mar 20 '24 at 19:21somescript some arguments >/dev/null 2>&1 & pid_mything1=$!
and you would have the PID of this particular script in$pid_mything1
to use later (passing it around in a script or even handing it to another script as an argument). You could do the same for any number of background tasks. You could even append the value of$!
in an array withmyscript & pids+=( $! )
. It is not clear from your question why you need to "tag" the processes in any special way. See also https://unix.stackexchange.com/questions/30370 – Kusalananda Mar 20 '24 at 19:24pids
will be gone by then if I understand correctly) – Adam Arold Mar 20 '24 at 19:30