Bash command output as variable returning /bin/bash - bash

When I run the following command in my terminal I get the expected value :
delayedjobs="$(ps aux | grep delayed_job | grep -v grep | awk '{print $11}' | awk 'END{print}' | cut -d "." -f2)" && echo $delayedjobs
However, when I run the following script, the variable returns as "/bin/bash"
#!/bin/bash
delayedjobs="$(ps aux | grep delayed_job | grep -v grep | awk '{print $11}' | awk 'END{print}' | cut -d "." -f2)"
echo ${delayedjobs}
root#central:/home/tblake# ./myscript.sh
/bin/bash
Can anyone explain this? Im having a heck of a time figuring this out.

Has you named your skript delayed_job ? Your script detect his own process.

Related

ps pipe and grep shell scripting illegal argument

New to shell scripting and just writing a little script to check if a process is running, if I use
PROCESS_NUM="ps -ef | grep '$1' | grep -v 'grep' | wc -l"
I get a "ps: illegal argument: |" error although if I echo out PROCESS_NUM and ctrl paste in the line it works just fine manually. Not sure why the | pipe is being troublesome here. Any help is much appreciated!
If you are trying to assig the output of ps -ef | grep '$1' | grep -v 'grep' | wc -l command to PROCESS_NUM variable, you need to use below:
PROCESS_NUM=`ps -ef | grep '$1' | grep -v 'grep' | wc -l`
or
PROCESS_NUM=$(ps -ef | grep '$1' | grep -v 'grep' | wc -l)
Using "", will treat the value inside of it as a string, except for some special character

Killing parent process only, not both child and parent in bash

I'm trying to kill process by name. It's supposed to kill children processes first then the parent later but I only get parent killed. Help needed please. EDIT: SOLVED
Please try it this way:
PID=$(ps -aef | grep `whoami` | grep $argument | grep -v grep | grep -v $$ | awk '{print $2}'
for x in ${PID[#]}; do
CPID=$(ps -aef | grep `whoami` | grep $PID | grep -v grep | grep -v $$ | awk '{print $3}'
for y in ${CPID[#]}; do
kill $y
done
kill $x
done

Something is wrong with unix script

Following is my script, every time I run this it goes into else part. when I run the TEST2EVAL command it gives me 1
#!/bin/sh
TEST2EVAL='ps auxf | grep some.jar | grep -v grep | wc -l'
if [ "$TEST2EVAL" = 1 ]
then
java -jar /path/to/jar &
else
echo "Running"
fi
Assuming you are trying to find out if any processes are running with some.jar on their command lines you probably want:
if pgrep -f some.jar; then
echo running;
else
echo not running;
fi
In in order save the output of a command in a variable, you have to enclose the command in backticks (`), not single quotes ('). Thus, change the second line of your script to:
TEST2EVAL=`ps auxf | grep some.jar | grep -v grep | wc -l`
You are using the wrong quotes for command substitution: not single quotes:
TEST2EVAL='ps auxf | grep some.jar | grep -v grep | wc -l'
but backquotes:
TEST2EVAL=`ps auxf | grep some.jar | grep -v grep | wc -l`
Better yet, use TEST2EVAL=$(ps auxf | grep some.jar | grep -v grep | wc -l) instead. It's much clearer, supported by all POSIX-compatible shells, and can be nested more easily when necessary.

commands not found when running script

I have a very basic script that keeps spitting back that the commands are not found. Ive looked all over this site and can not find an answer that works for me. The path to bash is correct. Ive checked the script with od. Ive run dos2unix. None of this helps me.
SCRIPT:
#!/bin/bash
HYBRISPROC=`ps -eo pid,command | grep [h]ybris | grep -v grep | awk '{print $1}'`
echo "Looking for Hybris..."
echo $HYBRISPROC
RESULTS:
./HybrisStopStart.sh: line 5: ps: command not found
./HybrisStopStart.sh: line 5: grep: command not found
./HybrisStopStart.sh: line 5: awk: command not found
./HybrisStopStart.sh: line 5: grep: command not found
Looking for Hybris...
Any ideas? If I run the command just on its own it works fine. Ive tried it as sudo as well and et the same results.
TIA
How about it?
#!/bin/bash
HYBRISPROC=`ps -eo pid,command | grep [h]ybris | grep -v grep | awk '{print $1}'`
echo "Looking for Hybris..."
echo "$HYBRISPROC"
(OR)
#!/bin/bash
HYBRISPROC="ps -eo pid,command | grep [h]ybris | grep -v grep | awk '{print $1}'"
echo "Looking for Hybris..."
bash -c "$HYBRISPROC"
(OR)
#!/bin/bash
HYBRISPROC="ps -eo pid,command | grep [h]ybris | grep -v grep | awk '{print $1}'"
echo "Looking for Hybris..."
eval "$HYBRISPROC"
TOTALY:
you can see the difference:
#!/bin/bash
LS=`ls -l`
echo $LS #nasty way
echo
echo "$LS" #good way
Try to add
PATH="$PATH:/usr/bin:/bin"
before code. Looks like bin directory is not on your path. So the commands are not found.

Machine data not displaying output every 15 Seconds

The following script is meant to output "vmstat" for example every 15 seconds, but for some reason it only does this if there's activity or when I kill the script, in other cases it just sits there.
#!/bin/bash
#!/bin/sh
ps -ef | grep -v grep | grep "vmstat 15" | awk '{ print $2 }' | xargs kill
ps -ef | grep -v grep | grep "iostat 15" | awk '{ print $2 }' | xargs kill
ps -ef | grep -v grep | grep "mpstat 15" | awk '{ print $2 }' | xargs kill
today=`date +%Y-%m-%d.%H:%M:%S`
find /var/log/ -name data_collection -type d -exec mv /var/log/data_collection /home/Beer/"data_collection_${today}" \;
mkdir -p /var/log/data_collection
vmstat 15 | /home/Beer/./addtimestamp.pl > /var/log/data_collection/vm_stat &
iostat 15 | /home/Beer/./addtimestamp.pl > /var/log/data_collection/ios_stat &
mpstat 15 | /home/Beer/./addtimestamp.pl > /var/log/data_collection/mp_stat &
Im guessing the '&' symbol at the end has something to do with this, I only did this so I can execute each command at once.
Since feature requests to mark a comment as an answer remain declined, I copy the above solution here.
What is in addtimestamp.pl? It could be that it's buffering input, and not flushing every time it reads something. – Diego Basch
#DiegoBasch that's exactly correct, I just added a '$|=1;' to the Perl script now and it started to work, basically setting the it to line buffer. Ill add the answer. – I AM L

Resources