How to find Sidekiq is running or not - ruby

In one of my project i am using Sidekiq
Is there any inbuilt Sidekiq console method/method that helps me to find whether sidekiq is running or not.
My requirement is kind of a pre check condition where if Sidekiq is not running i will raise a error.
I tried using the grep like
'ps -ef | grep sidekiq'
but it's not solving my purpose.
The method i am looking for should be something like:
Sidekiq.is_running?
Thanks in advance.
I also Tried
Sidekiq not running
1.9.3p392 :021 > system 'ps aux | grep sidekiq'
ankitgupta 6683 0.0 0.0 2432768 600 s001 R+ 11:47AM 0:00.00 grep sidekiq
ankitgupta 6681 0.0 0.0 2433432 916 s001 S+ 11:47AM 0:00.01 sh -c ps aux | grep sidekiq
=> true
Sidekiq is running
1.9.3p392 :022 > system 'ps aux | grep sidekiq'
ankitgupta 6725 0.0 0.0 2432768 600 s001 S+ 11:57AM 0:00.00 grep sidekiq
ankitgupta 6723 0.0 0.0 2433432 916 s001 S+ 11:57AM 0:00.00 sh -c ps aux | grep sidekiq
ankitgupta 6707 0.0 1.3 3207416 111608 s002 S+ 11:56AM 0:07.46 sidekiq 2.11.2 project_name [0 of 25 busy]
=> true
It is always returning true.. I want to catch the process when it runs

A little trick:
ps aux | grep '[s]idekiq'
Hope it works

Ideally you can do this directly from ruby itself. Put this in some rake task or standalone script (don't forget to specify Sidekiq connection details)
ps = Sidekiq::ProcessSet.new
ps.size # => 2
ps.each do |process|
p process['busy'] # => 3
p process['hostname'] # => 'myhost.local'
p process['pid'] # => 16131
end
ps.each(&:quiet!) # equivalent to the USR1 signal
ps.each(&:stop!) # equivalent to the TERM signal
From https://github.com/mperham/sidekiq/wiki/API#processes

See this question for how to filter ps output using grep, while eliminating the grep command from the the output.

I see, try this out:
module Process
class << self
def is_running?(pid)
begin
Process.kill(0, pid)
true
rescue Errno::ESRCH
false
end
end
end
end
1.9.3p392 :001 > puts `ps aux | grep -i [s]idekiq`
It'll return you pid like: 12247, and you can check if it's running:
Process.is_running?(12247) // true | false

You can add the following lines to your config/routes.rb
require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'
Start your server, open a browser and check your processess and your jobs
You can find it from Sidekiq Wiki

Related

Ansible doesn't return job ID for async task

According to the documentation when I run an async ad hoc command I should get a job ID back so I can check the status later. That's not happening:
$ ansible all -a "sleep 60" -P 0 -B 86400
node1 | SUCCESS | rc=-1 >>
node2 | SUCCESS | rc=-1 >>
node0 | SUCCESS | rc=-1 >>
node3 | SUCCESS | rc=-1 >>
$
The sleep command is running on all nodes, so the task does run correctly:
$ ssh node2 ps aux | grep sleep
$ ansible node2 --user ${USER} --background 3600 --poll 0 --args "/bin/sleep 10s"
node2 | SUCCESS | rc=-1 >>
$ ssh node2 ps aux | grep sleep
user 7232 0.0 0.0 7468 744 ? S 13:54 0:00 /bin/sleep 10s
$
But I have no way of checking the status with the async_status job since I don't know the job ID. Is there a way to list currently running async jobs? Or some other way to get the job ID?
Ubuntu 18.04.1, ansible 2.5.1, python 2.7.17.
According your output the return code is rc=-1 and the command sleep only without the full path.
On a RHEL7.9.9 system with Ansible 2.9.25 and Python 2.7.5 the output from the asynchronous ad-hoc task for a user looks like
ansible test --user ${USER} --ask-pass --background 3600 --poll 0 --args "/usr/bin/sleep 1m"
SSH password:
test1.example.com | CHANGED => {
"ansible_job_id": "211363790122.167971",
"changed": true,
"finished": 0,
"results_file": "/home/test/.ansible_async/211363790122.167971",
"started": 1
}
test2.example.com | CHANGED => {
"ansible_job_id": "289811848480.63295",
"changed": true,
"finished": 0,
"results_file": "/home/test/.ansible_async/289811848480.63295",
"started": 1
}
and for superuser like
ansible test --user ${USER} --ask-pass --become --background 3600 --poll 0 --args "/usr/bin/sleep 1m"
SSH password:
test1.example.com | CHANGED => {
"ansible_job_id": "494650580974.167671",
"changed": true,
"finished": 0,
"results_file": "/root/.ansible_async/494650580974.167671",
"started": 1
}
test2.example.com | CHANGED => {
"ansible_job_id": "273855897479.62987",
"changed": true,
"finished": 0,
"results_file": "/root/.ansible_async/273855897479.62987",
"started": 1
}
You may perform some tests with several different ad-hoc commands like
ansible test --user ${USER} --ask-pass --args "python --version"
SSH password:
test1.example.com | CHANGED | rc=0 >>
Python 2.7.5
test2.example.com | CHANGED | rc=0 >>
Python 2.7.5
with and without full path and so on. As well because of the rc=-1
ansible test --user ${USER} --ask-pass --module-name shell --args "echo test; echo rc=$?"
test1.example.com | CHANGED | rc=0 >>
test
rc=0
ansible test --user ${USER} --ask-pass --module-name shell --args "echo 'test '; exit -1;"
test1.example.com | FAILED | rc=255 >>
test non-zero return code

Unwanted too many arguments error being printed out

I am writing a bash script that logs into remote nodes and returns the services being run on that node.
#!/bin/bash
declare -a SERVICES=('redis-server' 'kube-controller-manager' 'kubelet' 'postgres' 'mongod' 'elasticsearch');
for svc in "${SERVICES[#]}"
do
RESULT=`ssh 172.29.219.109 "ps -ef | grep -v grep | grep $svc"`
if [ -z ${RESULT} ]
then
echo "Is Empty" > /dev/null
else
echo "$svc is running on this node"
fi
done
Now the output of ssh 172.29.219.109 "ps -ef | grep -v grep | grep $svc" on the node is ::
postgres 2102 1 0 Jan29 ? 00:24:27 /opt/PostgresPlus/pgbouncer/bin/pgbouncer -d /opt/PostgresPlus/pgbouncer/share/pgbouncer.ini
postgres 2394 1 0 Jan29 ? 00:20:10 /opt/PostgresPlus/9.4AS/bin/edb-postgres -D /opt/PostgresPlus/9.4AS/data
postgres 2431 2394 0 Jan29 ? 00:00:01 postgres: logger process
postgres 2434 2394 0 Jan29 ? 00:07:15 postgres: checkpointer process
postgres 2435 2394 0 Jan29 ? 00:01:10 postgres: writer process
postgres 2436 2394 0 Jan29 ? 00:03:27 postgres: wal writer process
postgres 2437 2394 0 Jan29 ? 00:20:03 postgres: autovacuum launcher process
postgres 2438 2394 0 Jan29 ? 00:37:00 postgres: stats collector process
postgres 2494 1 0 Jan29 ? 00:08:12 /opt/PostgresPlus/9.4AS/bin/pgagent -l 1 -s /var/log/ppas-agent-9.4.log hostaddr=localhost port=5432 dbname=postgres user=postgres
postgres 2495 2394 0 Jan29 ? 00:11:25 postgres: postgres postgres 127.0.0.1[59246] idle
When I run the script, I do get the result I want but Im getting an unwanted message which seems to be related to the variable in which I am storing my result.
# ./map_services_to_nodes.sh
./map_services_to_nodes.sh: line 12: [: too many arguments
postgres is found on this node
The Algo that I im using is ::
Search for all services defined in my array.
Store the result in a variable.
If Variable is empty, that means that service is not running.
If its not empty, service is running.
You need to escape $ (to avoid local expansion) and " when using inside ssh command, also avoid using the outdated back-ticks for command-substitution, use $(..), see Why Use $(STATEMENT) instead of legacy STATEMENT
RESULT=$(ssh 172.29.219.109 "ps -ef | grep -v grep | grep \$svc")
and double quote variables inside test operator,
if [ -z "${RESULT}" ]
Changed the below
if [ -z ${RESULT} ]
to
if [ -z "${RESULT}" ]
and it worked.
# ./map_services_to_nodes.sh
postgres is found on this node

how do I kill a port in use -- eventmachine

I get the following error:
eventmachine.rb:534:in `start_tcp_server': no acceptor (port is in use or requires root privileges)
I'm not sure what the process or port is and want to kill it so I can run my script again.
How do I do that?
ps ax | grep ruby
kill -9 [pid]
Example:
ps ax | grep ruby
#=> 857 pts/19 Sl+ 0:04 /home/andreydeineko/.rvm/rubies/ruby-2.2.3/bin/ruby bin/rails server
#=> 14639 pts/18 S+ 0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn rails
#=> 25368 pts/17 Sl+ 0:02 /home/andreydeineko/.rvm/rubies/ruby-2.2.3/bin/ruby bin/rails c
kill -9 857
ps ax | grep ruby
#=> 14639 pts/18 S+ 0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn rails
#=> 25368 pts/17 Sl+ 0:02 /home/andreydeineko/.rvm/rubies/ruby-2.2.3/bin/ruby bin/rails c

Parse command output with awk and count results

I have an output from the 'multipath -ll' command
From RHEL:
mpath114 (3600507680283095ea8000000000004fa) dm-28 IBM,2145
[size=200G][features=1 queue_if_no_path][hwhandler=0][rw]
\_ round-robin 0 [prio=50][active]
\_ 19:0:0:40 sdea 128:32 [active][ready]
\_ 20:0:1:40 sdeb 128:48 [active][ready]
\_ 20:0:1:41 sdec 128:16 [failed][faulty]
\_ round-robin 0 [prio=10][enabled]
\_ 20:0:0:40 sdba 67:64 [active][ready]
\_ 19:0:1:40 sdgg 131:192 [active][ready]
mpath131 (3600507680283095ea800000000000504) dm-39 IBM,2145
[size=10G][features=1 queue_if_no_path][hwhandler=0][rw]
\_ round-robin 0 [prio=50][active]
\_ 20:0:1:1 sdbl 67:240 [active][ready]
\_ 19:0:0:1 sdc 8:32 [active][ready]
\_ round-robin 0 [prio=10][enabled]
\_ 19:0:1:1 sdet 129:80 [active][ready]
\_ 20:0:0:1 sdk 8:160 [active][ready]
[..]
Or from SLES server:
mpathmzp (36005076801c7061ef800000000000089) dm-0 IBM,2145
size=10G features='1 queue_if_no_path' hwhandler='0' wp=rw
|-+- policy='round-robin 0' prio=50 status=enabled
| `- 67:0:2:0 sde 8:64 active ready running
| `- 68:0:0:1 sdl 8:76 failed faulty running
`-+- policy='round-robin 0' prio=10 status=enabled
|- 67:0:3:0 sdc 8:32 active ready running
`- 68:0:0:0 sdd 8:48 active ready running
[..]
I would like to parse it (preferably with awk or bash), to display summary of the configuration.
It should print the pseudo multipath device and the number of active paths and the failed (if any)
Sample:
dm-39, 10G, Total: 4 paths, active: 4, failed: 0
dm-28, 200G, Total: 5 paths, active: 4, failed: 1
Same for the SLES:
dm-0, 10G, Total: 4 paths, active: 3, failed: 1
If also possible, I'd like to sort the output so that the paths with no failed and most active paths are on top, and end with the devicess with the failed paths.
Thanks for helping!
This awk should do:
multipath -ll | awk 'NR>1 {r=f=0;for (i=1;i<=NF;i++) if ($i~/ready/) r++; else if ($i~/faulty/) f++;split($5,a,"=|]");print $3,a[2]"\tTotal: "r+f" paths, active: "r,"failed: "f}' RS="mpath" OFS=", "
dm-28, 200G Total: 5 paths, active: 4, failed: 1
dm-39, 10G Total: 4 paths, active: 4, failed: 0

"Can’t make \"0.0\" into type number." number -1700 from "0.0" to number

Why I keeping getting error "Can’t make \"0.0\" into type number." number -1700 from "0.0" to number? If I remove as number, the display dialog always showing.
tell application "System Events"
repeat
set PID to unix id of process "JPEGmini"
set getCpuPercent to "ps aux | grep " & PID & " | grep -v grep | awk '{print $3}'"
set cpuPercent to (do shell script getCpuPercent) as number
if (cpuPercent) < 5 then
display dialog cpuPercent
end if
end repeat
end tell
The problem here is your ps command is returning too much information. To illustrate what I mean, as I write, my google chrome process has a pid of 916. Using the approach in the question, I could do ps aux | grep 916 to see this process. But this is not sufficient - grep will match any instance of the string "916" in the ps output, so if there happens to be a process with a pid of say 1916 or 9160, then this will match as well. Also ps aux lists lots of other stats, many of which could also contain the string "916". In fact if I run ps aux | grep -c 916 there are currently 58 lines which match!
So all we need to do is tell ps we are only interested in a specific pid:
$ ps -o%cpu -p 916 | grep '[[:digit:]]'
0.5
$
This will list only the cpu% for the process with the given $PID. Piping to grep '[[:digit:]]' is necessary to return just the numeric percentage and strip the "CPU" column header from the ps output.
Wrapping this up into your original script you would have:
tell application "System Events"
repeat
set PID to unix id of process "JPEGmini"
set getCpuPercent to "ps -o%cpu -p " & PID & " | grep '[[:digit:]]'"
set cpuPercent to (do shell script getCpuPercent) as number
if (cpuPercent) < 5 then
display dialog cpuPercent
end if
end repeat
end tell
I don't have JPEGmini installed, but this works fine for me on all other processes I have tried on my OSX 10.8.5 powerbook.
I tried your script with the process Safari and also got the error. It seems there was more than one result being returned from the "do shell script" line... so it couldn't make the result into a number.
I changed the code to this and it worked...
tell application "System Events"
repeat
set PID to unix id of process "Safari"
set getCpuPercentCmd to "ps aux | grep " & PID & " | grep -v grep | awk '{print $3}'"
set getCpuPercent to paragraphs of (do shell script getCpuPercentCmd)
set cpuPercent to (item -1 of getCpuPercent) as number
if cpuPercent < 5 then
display dialog cpuPercent as text
end if
end repeat
end tell
I ran just the grep command and found that on my machine webkit is running a WebProcess that is tied to the Safari PID. You can see toward the end of the second line that the -servicename is com.apple.WebKit.WebProcess-4881-0x1076eb0c0. Because of that, the grep is actually finding two results and returning "0.0\r0.0" which it cannot make into a number.
$user 4881 13.3 1.2 3812416 104840 ?? R 4:27PM 0:09.57 /Applications/Safari.app/Contents/MacOS/Safari -psn_0_1692061
$user 4885 0.1 0.7 3778328 56108 ?? S 4:27PM 0:00.79 /System/Library/StagedFrameworks/Safari/WebKit2.framework/WebProcess.app/Contents/MacOS/WebProcess /System/Library/StagedFrameworks/Safari/WebKit2.framework/WebKit2 -type webprocess -servicename com.apple.WebKit.WebProcess-4881-0x1076eb0c0 -localization en_US -client-identifier com.apple.Safari -ui-process-name Safari
$user 5250 0.0 0.0 2432768 520 ?? R 4:29PM 0:00.00 grep 4881
$user 5248 0.0 0.0 2433432 824 ?? S 4:29PM 0:00.00 sh -c ps aux | grep 4881

Resources