Every process will be listed in the output of ps aux
; whether running, sleeping, zombie or stopped.
However, in your case, since you ran the process using sh abc.sh
, sh
is the application(shell) that is running and not abc.sh
. Hence, ps aux
will not contain the process abc.sh
because of which grep
could not yield any result.
So, the correct way you should have used it is as:
ps aux | grep sh
This may also return you other process that are running having the string sh
anywhere in their output of ps aux
.
You should note that the process will be "running" when the output of ps aux
has its STAT
as R
. If it is something other than that, it is not running at the instance you fired the command to check the running processes. The different process states can be found in the man page for ps:
D uninterruptible sleep (usually IO)
R running or runnable (on run queue)
S interruptible sleep (waiting for an event to complete)
T stopped, either by a job control signal or because it is being traced
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z defunct ("zombie") process, terminated but not reaped by its parent
You could as well run the top
command to check if the process is running or sleeping and the amount of CPU, RAM it is consuming. (This will again list your process as sh
).
However, if you do want your process to be listed as abc.sh
, then you should have the first line of the script you are running as:
#!/bin/sh
so that the shell will know what application to use to run the script(sh in this case, change it to #!/bin/bash
for bash) and then provide executable permissions to the process using:
chmod +x /path/to/abc.sh
replacing /path/to/
with the location of the abc.sh
file and then run abc.sh
using
/path/to/abc.sh
again replacing /path/to/
with the location of the abc.sh
file.
In general there are more appropriate ways of parsing JSON objects, but since in this case the JSON object is very simple you may store curl
's output in a variable (which is possible) and just use AWK:
var="$(curl ipinfo.io/"8.8.8.8" 2>/dev/null)"
<<<"$var" awk -F'"' '$2=="city"{printf("%s, ", $4)}$2=="region"{print $4}'
% var="$(curl ipinfo.io/"8.8.8.8" 2>/dev/null)"
% <<<"$var" awk -F'"' '$2=="city"{printf("%s, ", $4)}$2=="region"{print $4}'
Mountain View, California
However unless you want to use curl
's output multiple times you may just use a pipe:
curl ipinfo.io/"8.8.8.8" 2>/dev/null | awk -F'"' '$2=="city"{printf("%s, ", $4)}$2=="region"{print $4}'
curl ipinfo.io/"8.8.8.8" 2>/dev/null | awk -F'"' '$2=="city"{printf("%s, ", $4)}$2=="region"{print $4}'
Mountain View, California
<<<
is a form of input redirection called "here string"; it redirects the STDIN of a command from the terminal to a string.
What happens here is that $var
is expanded between the double quotes; the STDIN of the AWK command is redirected from the terminal to the expanded string and AWK consequently reads the string as its input file.
Best Answer
Basically, you want this structure
All it does is to silently (without printing to screen) check if there is a match of string "unclean" in the file . If there is we do the if part, otherwise - we do else part.
Here's an example:
What also can be done is to start command you want from script, but in background. That way we can have it's PID. Here's what I mean
Adding
&
causesecho "HelloWorld"
run in background, and we have it's PID stored in$!
variable. Thus, we can do something like,