I have one script called test.sh. This calls another script foo.sh, like so:

echo "starting other script"
. ./foo.sh

The foo.sh script looks like this:

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?

  • The 'dot' (.) or 'source' command does not create a separate process; it runs the commands from the specified file in the same, existing shell (and ignores any shebang, BTW). You cannot see this in the process status. You can see any nonbuiltin commands currently executing, here the sleep commands. You could look with lsof, fuser, or on some Unix variants /proc/pid/fd/*, whether a shell has foo.sh open, but that doesn't prove it's being executed since a shell can read any file either as commands or data. Oct 25 '19 at 16:38

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 the source 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 using ps.

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. When foo.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.

but how can I check if foo.sh is running at all?

This is easy from the script that launched foo.sh because it waits until foo.sh completes before resuming its execution.

It is probably impossible (or almost there) from outside.

  • Thanks @axiac. Great answer. If i launch foo.sh as './foo.sh', instead of '. ./foo.sh' will test.sh wait until foo.sh is completed? I think i'm in a good position, because foo.sh doesn't rely on any environment variables contained in test.sh at all, right?
    – njk2015
    Oct 26 '19 at 9:14
  • If you launch foo.sh as ./foo.sh or as . ./foo.sh the calling script waits until foo.sh completes. If you want to launch a command in background (i.e. do not wait for it to complete), add & after the command line (e.g. ./foo.sh &).
    – axiac
    Oct 26 '19 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.