I am currently writing a script that is writing scripts and then executing them.
The user that is running the parent script is the root user.
Here is the code:
1 for i in $(seq 1 $num)
2 do
3
4
5 scriptL1="#!/bin/bash"
6 scriptL2="i=$i"
7 echo $scriptL1 > /tmp/pulse$i.sh
8 echo $scriptL2 >> /tmp/pulse$i.sh
9
10 #this loop writes line-by-line from a file to the child script file
11 for j in $(seq 1 $(cat $HOME/mainscript.txt | wc -l))
12 do
13 line=$(head -n $j $HOME/mainscript.txt | tail -n 1)
14 echo $line >> /tmp/pulse$i.sh
15 done
16
17 chmod +x /tmp/pulse$i.sh
18
19 setsid /tmp/pulse$i.sh &
20 done
What it is essentially doing is for $num number of items in a directory, it makes $num number of scripts in /tmp. Each script made in /tmp from this script works as expected. In fact, this overall script works. However, when I run this parent script from the terminal, it fails to start any /tmp/pulseVM$i.sh executables.
In short, this script fails to execute line 19 as expected and I have no idea why.
I am expecting that this parent script will fork these child scripts to the background. That way, this parent script can end while the child scripts continue. Currently, child scripts aren't being executed.
Here is a sample mainscript.txt:
WORK="#Write a directory path here"
#This directory is owned by root and must be owned by the root. It is also shared with a virtual machine that is using root.
rm $WORK/VMInputShared/VM$i/pong/pulse
touch $WORK/VMInputShared/VM$i/ready
sleep 10
fails=0
while true
do
if [[ $(ls $WORK/VMInputShared/VM$i/pong) == "pulse" ]]
then
touch $WORK/VMReadyKeys/VM$i
fails=0
mv $WORK/VMInputShared/VM$i/pong/pulse $WORK/VMInputShared/VM$i/ping/pulse
else
((fails++))
if [ $fails -le 9 ]
then
echo "Failure $fails/10..."
else
echo "Failure $fails/10..."
rm $WORK/VMReadyKeys/VM$i
fi
fi
sleep 3.2
done
It is important to note that .../VM$i is a shared folder with a virtual machine running on this system. The virtual machine is running under root and due to the purposes of the virtual machine, it needs root privileges at all times.