0

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.

Adam Arold
  • 175
  • 2
  • 3
  • 12
  • 1
    Try using screen to start the script in the background. – user68186 Mar 20 '24 at 19:00
  • Possibly related: https://askubuntu.com/questions/622124/how-to-execute-an-script-as-daemon-with-nohup/622143#622143 – Kusalananda Mar 20 '24 at 19:12
  • @Kusalananda not related, as I know in advance the names of these services and I want to "tag" the background process with this name. I have no access to the pid (or any other data) after my script returns. – Adam Arold Mar 20 '24 at 19:20
  • @user68186 how does screen work? – Adam Arold Mar 20 '24 at 19:21
  • 1
    @AdamArold But according to the code in your question, you could just use somescript 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 with myscript & 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:24
  • This is just a small extract from the code I have. I have a script that displays a list of all the services (using whiptail checkboxes), and the user can toggle which services to have running. This script terminates leaving all the variables behind until the user calls the script again (so pids will be gone by then if I understand correctly) – Adam Arold Mar 20 '24 at 19:30
  • @AdamArold This is definitely not clear from the question. – Kusalananda Mar 20 '24 at 19:31
  • 1
    @Kusalananda I updated my question. – Adam Arold Mar 20 '24 at 21:50

0 Answers0