0

startScript.sh is in /root/script/startScript.sh

script1.shis in /root/script/test/script1.sh

script2.sh is in /root/script/test/script2.sh

startScript.sh looks like below

#!/bin/bash
#some code!
sh `pwd`/test/script1.sh 2>&1 &
sh `pwd`/test/script2.sh 2>&1 &
#some code

script2.sh and script1.sh look like below

#!/bin/bash
> `pwd`/process_ids.txt
while true;do
  echo "The Process: `ps`" >> `pwd`/process_ids.txt
  #some code to parse the process, etc. and echo it
done

Here is the thing is, the process_ids.txt file is creating in /root/script. But according to scriptx.sh the pwd is /root/scripts/test/. I displayed the pwd in scriptx.sh, it is showing /root/script/. How can I get the pwd of scriptx.sh?

1

Why don't you just add test/ to the path of the file you're trying to create, i.e.

#!/bin/bash
> `pwd`/test/process_ids.txt
while true;do
  echo "The Process: `ps`" >> `pwd`/test/process_ids.txt
  #some code to parse the process, etc. and echo it
done
0

The simplest approach is to pass it as a parameter to scripts 1 and 2. So, modify scriptx.sh to (by the way, use $PWD, there's no reason to run an external command):

#!/bin/bash
#some code!
sh "$PWD"/test/script1.sh "$PWD" 2>&1 &
sh "$PWD"/test/script2.sh "$PWD" 2>&1 &
#some code

And script1.sh and script2.sh to:

#!/bin/bash

dir="$1"
> "$dir"/process_ids.txt
while true;do
  echo "The Process: `ps`" >> "$dir"/process_ids.txt
  #some code to parse the process, etc. and echo it
done

The more complex approach is to get the PWD of the parent script from the /proc filesystem. Since scriptx.sh is launching the others, they will have access to scriptx.sh's PID in the $PPID variable. In Linux, each running process has a link to its current working directory at /proc/PID/cwd. Therefore, you could leave scriptx.sh unchanged and modify the others to:

#!/bin/bash

dir="$(readlink -f /proc/$PPID/cwd)"
> "$dir"/process_ids.txt
while true;do
  echo "The Process: `ps`" >> "$dir"/process_ids.txt
  #some code to parse the process, etc. and echo it
done
  • Thanks for reply. But, in scriptx.sh, the $1 has value /root/script...still the script is creating the the file in /root/script. We can send the argument as "pwd/test" in startScript.sh to scriptx.sh. But I don't want to do like that!...Is there any other way? – Veerendra Nov 26 '15 at 12:56
  • @Veerendra with my first suggestion, there is no $1 in scriptx.sh. That's only in script1.sh and script2.sh. Passing $PWD to the daughter scripts is really is the simplest way. Still, see update for another approach. – terdon Nov 26 '15 at 13:00
0

you don't need to get or pass anybody anything. that's already done. the current working directory of a forked process is the current working directory of the parent process. so, unless those scripts are changing their current directory, and if they are it is either intended behavior or something you should stop them doing with an edit, then the current working directory is the same for everybody. but if there were a difference for some reason, then the output of...

`pwd`

...would be just as problematic and for exactly the same reasons, and so could not be considered as any indication one way or the other.

still, if you did want to check these things for another process, and you had the other process's PID - as you do for script2.sh in the shell parameter $! and which you can get for the other with jobs -p, then, on a linux system, you could do:

cd -P /proc/"$!"/cwd;pwd;cd -

...which will first change to the last backgrounded process's current working directory, then print it, then change back to the current shell's previous current working directory, and print it.

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