Reputation: 881
Hi this script works fine, it calls the function when there a cronjob running under root
usage ()
{
# Print usage
echo "usage function"
}
#
export -f usage
ps -ef | awk '{if ($0 ~ /crond/ && $1 == "root") {system("/usr/bin/env bash -c usage") }}'
However, in addition to calling the function I also like to print $2 or just call a second command. I cannot get the syntax right. Can someone help please. in short, how do you run multiple commands when the condition is met? In the example above, in addition to running the usage() function I also like to run a second function and also print $2 ( which is the process ID ). Thanking you all in advance
Upvotes: 0
Views: 52
Reputation:
For Bash, you can use ;
or &&
to chain together commands.
;
is used to separate commands, but does not check the error code of the previous command. e.g. dddate; echo 'hi'
will echo 'hi', even though "dddate" likely returns an error.
&&
will only run the latter command if the previous command doesn't throw an error. e.g. dddate && echo 'hi'
will not run the echo
command because dddate
failed.
For your usage, you could make:
system("/usr/bin/env bash -c usage")
into system("/usr/bin/env bash -c usage); print $2"
.
root@debian:~# ps -ef | awk '{if ($0 ~ /crond/ && $1 == "root") {system("/usr/bin/env bash -c usage"); print "hi"}}'
usage function
hi
Upvotes: 2
Reputation: 592
Try running multiple commands separated by semicolon
For example :
ps -ef | awk '{if ($0 ~ /crond/ && $1 == "root") {system("/usr/bin/env bash -c usage"); system("/usr/bin/env bash -c func2"); print $2 }}'
Upvotes: 1