V
V
Vladimir Korshunov2021-02-12 22:07:54
linux
Vladimir Korshunov, 2021-02-12 22:07:54

What is the difference between ps axu and ps cax?

I want to make it so that when my telegram bot crashes, it starts automatically again. Started writing a bash script. First you need to check if such a process is running. I wanted to do it like this:

#!/bin/bash
bot_pid=`cat /home/vladimir/bot_dir/pid.txt`
echo $bot_pid
ps axu | grep $bot_pid
if [ $? == 0 ]; then echo "Bot is working"
elif [ $? == 1 ]; then echo "Bot is dead"
fi

After that, I went to the Internet and found almost exactly the same code, only the ps cax command was used there. I already understood why my version does not work - it just finds its own request in the list, gets 0 and says that the bot is working. I've tried googling what the c option does, but I still don't understand why it suddenly worked.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BorLaze, 2021-02-12
@BorLaze

Actually, it's better to do this through systemd
PS: A short googling shows that you can find out if a process with a given pid is running with the command 0 - it works (returns a command), 1 - there is no such process
ps -p $bot_pid -o comm=

S
Saboteur, 2021-02-13
@saboteur_kiev

man ps
is it really that hard to find out what the c and u options do?
one outputs the entire command, the other is user-oriented
But in general, if you already have a PID file, then it's better

ps -p $(cat /home/vladimir/bot_dir/pid.txt)
if [ $? == 0 ]; then echo "Bot is working"
elif [ $? == 1 ]; then echo "Bot is dead"
fi

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question