I have one script called test.sh. This calls another script foo.sh, like so:
#!/bin/bash
echo "starting other script"
. ./foo.sh
The foo.sh script looks like this:
#!/bin/bash
sleep 10
echo "running"
sleep 10
Now, when I do the following from the terminal:
ps -def | grep foo.sh | grep -v grep
I never get a result. I can see the output from the scripts in the terminal, no problem, but how can I check if foo.sh is running at all?
Best Answer
You launch
foo.sh
as. ./foo.sh
.A shell script is not a process by itself. It is interpreted by an instance of the shell, which is the process.
.
is an alias of thesource
command that executes commands in the current shell environment. This means./foo.sh
is executed by the same shell that executes the script that launches it. It does not create a new process and this is why you cannot find it usingps
.You can launch
foo.sh
as./foo.sh
. In this case it will be executed by a new instance of the shell, i.e. into a new process.But, depending on what it does, the execution of the original shell after
./foo.sh
completes might be different in the two cases. Whenfoo.sh
is executed in the same shell, the environment variables it changes belong to the shell that also executes the original script. Changing them could affect the original script.When you run
foo.sh
in a separate script the changes it operates on the environment variable do not affect the original script. They are different processes, the do not share anything.This is easy from the script that launched
foo.sh
because it waits untilfoo.sh
completes before resuming its execution.It is probably impossible (or almost there) from outside.