It is clear from you edit that the second script is called enigma2.sh
, and that you call it by writing enigma2.sh
on the command line. The script is somewhere in your path (you declare that it is at /usr/bin/enigma2.sh
, and I shall assume that /usr/bin
is in your PATH variable). Combining that the script could be found (in the PATH) by the shell and that the first line of the script is #!/bin/sh
, the kernel will start a new process that has a command line of:
/bin/sh enigma2.sh
You can confirm this by simply making the first lines of that script as this:
#!/bin/sh
ps ax|grep enigma2
exit 0
...
You don't need to erase the script, just place the lines shown above at the start of the script and leave all the rest below.
Run it to get:
$ enigma2.sh
3109 pts/6 S+ 0:00 /bin/sh enigma2.sh
3111 pts/6 S+ 0:00 grep enigma2
There are two processes that include the word enigma2
.
That explains part of the problem. Now, if you change the script to:
#!/bin/sh
enigma_pid=$(ps ax|grep enigma2)
echo "$enigma_pid"
exit 0
... # leave the old script below (no need to erase it).
And run it:
$ enigma2.sh
3509 pts/6 S+ 0:00 /bin/sh ./enigma2.sh
3510 pts/6 S+ 0:00 /bin/sh ./enigma2.sh
3512 pts/6 S+ 0:00 grep enigma2
When you use a $(…)
one additional process appear.
Removing the grep process with grep -v grep
leaves 2 processes.
That is the number you get 2
.
Note: Please, don't use uppercase variable names, those are reserved for environment variables.
Don't use ENIGMA_PID
Solutions
First: If you are going to identify a process by its name, don't use the exact same name in other programs or scripts. You may call the script enigma33.sh
.
Second: In this case, you can restrict grep to match a name that ends at 2
. The script ends in .sh
and won't be counted. Use
grep '[e]nigma2$'
And, since all you need is the count of matching lines use (-c
):
enigma_pid=$(ps ax | grep -c '[e]nigma2$')
Yes, no need for grep -v grep
since the regex [e]nigma2$
can not be equal (as an string) to enigma2
.
Edited script:
#!/bin/sh
# Detect If enigma2 is running.
enigma_pid=$(ps ax|grep -c '[e]nigma2$')
echo "$enigma_pid"
# Deetct if minisatip is running.
mini_pid=$(ps ax|grep -c '[m]inisatip$' )
# Check if the no_enigma file exists
if [ -f /home/root/no_enigma ]; then
if [ -z "${enigma_pid##*[!0-9]*}" ] ; then
echo "error: Not a number" >&2; exit 1
else
echo "It IS a number"
fi
if [ "$enigma_pid" -gt 0 ]; then
echo "$enigma_pid"
echo "enigma2 is running"
killall -r '[e]nigma2$' # make killing more restrictive
else
echo "enigma2 is not running"
fi
# Check if minisatip is running already, if not then start it
if [ "$mini_pid" -gt 0 ]; then
echo "minisatip is already running"
else
echo "minisatip is not running"
echo "starting minisatip"
/usr/bin/minisatip --satip-xml http://127.0.0.1:8554 -R /usr/share/minisatip/html
fi
else
# The no_enigma file does not exist
# Check if minisatip is running, if yes then kill it
if [ "$mini_pid" -gt 0 ]; then
echo "minisatip is running, killing it now"
killall minisatip
else
echo "minisatip is not running"
fi
# Check if enigma2 is running already, if not then start it
if [ "$enigma_pid" -gt 0 ]; then
echo "enigma2 is already running"
else
echo "enigma2 is not running"
echo "starting enigma2"
/home/root/run_enigma.sh
fi
fi
Best Answer
That looks like an error, if the line is to be interpreted by itself.
A backslash escapes the next character from being interpreted by the shell. If the next character is a newline, then the newline will not be interpreted as the end of the command by the shell. It effectively allows the a command to span multiple lines.
It is most commonly used in situations like these (i.e. to make it easier to read a long command):
Notice that the last line does not end with
\
since it is not continued on the next line. Also note that nothing (not even a comment or a single space) may follow the\
on the lines that are broken up in this way.Apart from that, yes, the command downloads a file and saves it to a file on disk. Another way of doing the same thing, without a redirection, would be to us Curl's
-o
option:(followed by whatever is on the next line in your script)