Bitcoin Forum
May 28, 2024, 02:38:05 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1] 2 »  All
  Print  
Author Topic: How to repeat the same command automatically in Linux?  (Read 199 times)
BTC Turkiye (OP)
Sr. Member
****
Offline Offline

Activity: 472
Merit: 254


Anlik Coin Fiyatlari BTCkur.com


View Profile WWW
March 01, 2018, 01:48:34 AM
 #1

I have a bot which gives an error sometimes and cancels the process then I`ll have to the run command again to re-start the bot.

Is there anyway to automatically re-run the same previous command if the process is cancelled and it goes back to the main terminal command line again?

I use Debian

Thank you in advance for any help

Farklı Borsaların Anlık Bitcoin, Litecoin, Ethereum ve Bitcoin Cash Kurunu Takip Edebilirsiniz.
BTCkur.com
BTC Turkiye (OP)
Sr. Member
****
Offline Offline

Activity: 472
Merit: 254


Anlik Coin Fiyatlari BTCkur.com


View Profile WWW
March 01, 2018, 09:59:43 AM
 #2

No one can help me on this?

Farklı Borsaların Anlık Bitcoin, Litecoin, Ethereum ve Bitcoin Cash Kurunu Takip Edebilirsiniz.
BTCkur.com
HeRetiK
Legendary
*
Offline Offline

Activity: 2940
Merit: 2092



View Profile
March 01, 2018, 10:05:41 AM
 #3

Usually you can use the arrow keys up / down to navigate through your command history, if that's what you're looking for. This is true for both Linux and Windows machines. Or are you talking about writing a script that repeats the most recent command in case of a failure?
BTC Turkiye (OP)
Sr. Member
****
Offline Offline

Activity: 472
Merit: 254


Anlik Coin Fiyatlari BTCkur.com


View Profile WWW
March 01, 2018, 10:07:36 AM
 #4

Are you talking about writing a script that repeats the most recent command in case of a failure?

That`s exactly what I`m looking for. Anytime it comes back to the command line, it should repeat the same command that previously just ran

Farklı Borsaların Anlık Bitcoin, Litecoin, Ethereum ve Bitcoin Cash Kurunu Takip Edebilirsiniz.
BTCkur.com
BTC Turkiye (OP)
Sr. Member
****
Offline Offline

Activity: 472
Merit: 254


Anlik Coin Fiyatlari BTCkur.com


View Profile WWW
March 01, 2018, 07:10:27 PM
 #5

Usually you can use the arrow keys up / down to navigate through your command history, if that's what you're looking for. This is true for both Linux and Windows machines. Or are you talking about writing a script that repeats the most recent command in case of a failure?

Love your Motto BTW Cheesy
Awesome!

Farklı Borsaların Anlık Bitcoin, Litecoin, Ethereum ve Bitcoin Cash Kurunu Takip Edebilirsiniz.
BTCkur.com
HeRetiK
Legendary
*
Offline Offline

Activity: 2940
Merit: 2092



View Profile
March 01, 2018, 10:52:46 PM
 #6

Are you talking about writing a script that repeats the most recent command in case of a failure?

That`s exactly what I`m looking for. Anytime it comes back to the command line, it should repeat the same command that previously just ran

Depending on your setup you either want to have a utility like daemontools watch the process and restart it on a crash:

https://unix.stackexchange.com/questions/336501/how-to-restart-a-process-automatically-when-it-killed-in-linux-centos


Or add a loop to your script that retries the same command until it succeeds:

https://serverfault.com/questions/80862/bash-script-repeat-command-if-it-returns-an-error
https://stackoverflow.com/questions/12321469/retry-a-bash-command-with-timeout

In case you're new to shell scripting: When an application (or command) runs successfully it returns a status code = 0; if it fails it returns a nonzero error code. These loops retry the same command with a slight delay inbetween (sleep) until it returns the status code = 0, ie. succeeds.
BTC Turkiye (OP)
Sr. Member
****
Offline Offline

Activity: 472
Merit: 254


Anlik Coin Fiyatlari BTCkur.com


View Profile WWW
March 04, 2018, 06:38:19 AM
 #7

Are you talking about writing a script that repeats the most recent command in case of a failure?

That`s exactly what I`m looking for. Anytime it comes back to the command line, it should repeat the same command that previously just ran

Depending on your setup you either want to have a utility like daemontools watch the process and restart it on a crash:

https://unix.stackexchange.com/questions/336501/how-to-restart-a-process-automatically-when-it-killed-in-linux-centos


Or add a loop to your script that retries the same command until it succeeds:

https://serverfault.com/questions/80862/bash-script-repeat-command-if-it-returns-an-error
https://stackoverflow.com/questions/12321469/retry-a-bash-command-with-timeout

In case you're new to shell scripting: When an application (or command) runs successfully it returns a status code = 0; if it fails it returns a nonzero error code. These loops retry the same command with a slight delay inbetween (sleep) until it returns the status code = 0, ie. succeeds.


Ok. I`m a little confused now. I`m still very much of a n00b in Linux

I`ve been trying this daemontools thing but I couldn`t figure out how to install it on Debian or if it comes in default already.

So I moved to the other links you sent but they`re all in different codes and I`m not sure what to do with them. My script is in Python and it keeps running until sometimes it gives and error and close itself. I just want it to start again with the same command when it closes. So should I add those codes in the python files? if so which file?
or should I create a new file and add it to the scripts folder or something?

I`d really appreciate it if you can explain step by step what to do. I desperately need this

Thank you

Farklı Borsaların Anlık Bitcoin, Litecoin, Ethereum ve Bitcoin Cash Kurunu Takip Edebilirsiniz.
BTCkur.com
Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
March 04, 2018, 09:29:58 AM
 #8

Does your script create a pidfile in eg /var/run? If so, you can use a cronjob to see if the pid still exist ("kill -0 `cat /var/run/myprocess.pid`").
If there is no pidfile, you can use top to see if the process is still running ("pidof myprocess" or "ps -x | grep myprocess | grep -v grep | awk {'print $1'}")

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
BTC Turkiye (OP)
Sr. Member
****
Offline Offline

Activity: 472
Merit: 254


Anlik Coin Fiyatlari BTCkur.com


View Profile WWW
March 04, 2018, 09:31:02 AM
 #9

I tried the PID method but PID changes everytime it runs

Farklı Borsaların Anlık Bitcoin, Litecoin, Ethereum ve Bitcoin Cash Kurunu Takip Edebilirsiniz.
BTCkur.com
Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
March 04, 2018, 12:38:33 PM
 #10

Well sure it does. You get a new pid assigned every time.
When you check it and see that it's not running, then your watchdog script just needs to run the command which starts your bot again after it crashed.

If your bot is running as a service, you might want to look into systemd; afaik you can set in a unit file that it automatically restarts a service.

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
BTC Turkiye (OP)
Sr. Member
****
Offline Offline

Activity: 472
Merit: 254


Anlik Coin Fiyatlari BTCkur.com


View Profile WWW
March 04, 2018, 12:44:13 PM
 #11

Well sure it does. You get a new pid assigned every time.
When you check it and see that it's not running, then your watchdog script just needs to run the command which starts your bot again after it crashed.

If your bot is running as a service, you might want to look into systemd; afaik you can set in a unit file that it automatically restarts a service.


I don`t think I explained it right.
When I run the script now the PID is 23423 and the script I will run on cron is going to be like waht you wrote above. But when I`m not around and the script dies. Your code will look for that PID but that PID will disappear and wont exist anymore.

So how can your code run a script automatically when the PID changes every time it runs?

What am I going to write at this part of your code exactly?
Code:
"pidof myprocess" 

Farklı Borsaların Anlık Bitcoin, Litecoin, Ethereum ve Bitcoin Cash Kurunu Takip Edebilirsiniz.
BTCkur.com
Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
March 04, 2018, 01:04:09 PM
 #12

Your code will look for that PID but that PID will disappear and wont exist anymore.
No, the code checks either if a pid exists (given via eg a pidfile) or if a given process exists by name (as in, has a pid).
You don't know the pid before you start a process.

From the top of my head, you could try something like that as a cronjob:
Code:
#!/usr/bin/bash
if [[ ! $(pidof yourbot) ]]; then
echo "Bot down"
#command to restart your bot
fi

Look into the processlist to see under what name your bot is running (with "ps ax" for example).
Let's say it's "yourbot": then "pidof yourbot" will always return a pid, unless your bot has crashed and its process is gone.
As a side note: I assumed your bot is running only once; otherwise the approach needs to be changed a bit.

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
BTC Turkiye (OP)
Sr. Member
****
Offline Offline

Activity: 472
Merit: 254


Anlik Coin Fiyatlari BTCkur.com


View Profile WWW
March 04, 2018, 01:15:42 PM
 #13

The confusing thing is

1- Should I create a file and name it test.sh and then put this in cron job? Will that just work like that?
2- Process name shows as just python. But what I write in terminal is like
Code:
python rtgferfd.py --solid --r --fr --quantity 35 --wait_time 34

Farklı Borsaların Anlık Bitcoin, Litecoin, Ethereum ve Bitcoin Cash Kurunu Takip Edebilirsiniz.
BTCkur.com
Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
March 04, 2018, 01:53:12 PM
 #14

If the command pgrep exists, try this:
Code:
#!/usr/bin/bash
if [[ ! $(pgrep -f "rtgferfd.py") ]]; then
echo "Bot down"
python rtgferfd.py --solid --r --fr --quantity 35 --wait_time 34 &
fi

If not, this:
Code:
#!/usr/bin/bash
if [ "$(ps ax | grep "rtgferfd.py" | grep -vc grep)" -eq "0" ]; then
echo "Bot down"
python rtgferfd.py --solid --r --fr --quantity 35 --wait_time 34 &
fi

Make sure you use full paths though, or you might run into troubles with restarting.
This script should run fine via cron. Put it into a file and tell cron to run it eg every minute.
Try it manually first: name it test.sh or whatever you want to call it and execute it.
If your bot is running, you should see not output. If it is not, your bot should appear in the processlist afterwards.

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
BTC Turkiye (OP)
Sr. Member
****
Offline Offline

Activity: 472
Merit: 254


Anlik Coin Fiyatlari BTCkur.com


View Profile WWW
March 04, 2018, 01:56:27 PM
 #15

If the command pgrep exists, try this:
Code:
#!/usr/bin/bash
if [[ ! $(pgrep -f "rtgferfd.py") ]]; then
echo "Bot down"
python rtgferfd.py --solid --r --fr --quantity 35 --wait_time 34 &
fi

If not, this:
Code:
#!/usr/bin/bash
if [ "$(ps ax | grep "rtgferfd.py" | grep -vc grep)" -eq "0" ]; then
echo "Bot down"
python rtgferfd.py --solid --r --fr --quantity 35 --wait_time 34 &
fi

Make sure you use full paths though, or you might run into troubles with restarting.
This script should run fine via cron. Put it into a file and tell cron to run it eg every minute.
Try it manually first: name it test.sh or whatever you want to call it and execute it.
If your bot is running, you should see not output. If it is not, your bot should appear in the processlist afterwards.


You`re awesome!
I`ll try to see if it`ll work like this!
Thank you very very much!

Farklı Borsaların Anlık Bitcoin, Litecoin, Ethereum ve Bitcoin Cash Kurunu Takip Edebilirsiniz.
BTCkur.com
BTC Turkiye (OP)
Sr. Member
****
Offline Offline

Activity: 472
Merit: 254


Anlik Coin Fiyatlari BTCkur.com


View Profile WWW
March 05, 2018, 04:17:12 PM
 #16

It worked perfectly like below. But I have couple questions


1- Does this script checks the code every some time? because I see my script is running but then once every minute or so I see one more of the same python process goes on the list for couple seconds and then disappear.

2- I didn`t need to add it to cron job because when I run your code, it checks the script and can`t find it so It starts it and when I cancel it, it restarts it again. So as long as your code runs, the script keeps running no matter what. Is that normal and should I still add the .sh file to the cron job?

Code:
#!/usr/bin/bash
if [[ ! $(pgrep -f "rtgferfd.py") ]]; then
echo "Bot down"
python rtgferfd.py --solid --r --fr --quantity 35 --wait_time 34 &
fi

Farklı Borsaların Anlık Bitcoin, Litecoin, Ethereum ve Bitcoin Cash Kurunu Takip Edebilirsiniz.
BTCkur.com
Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
March 05, 2018, 07:15:59 PM
 #17

1: No, it only checks when you execute it. It's a one-time check. If you see more processes, maybe your bot uses forks/threads.

2: Adding it to cron means the script will be run in the intervals you define, 24x7. So if you want your bot to be checked even when you're asleep, add it. But you will have to use full paths or it will fail via cron. As long as the bot runs you won't see anything, but when it gets restarted, cron should send off an email to you (if it's configured that way) and restart it.

Or, you could find out what causes the bot to crash and fix that.

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
BTC Turkiye (OP)
Sr. Member
****
Offline Offline

Activity: 472
Merit: 254


Anlik Coin Fiyatlari BTCkur.com


View Profile WWW
March 08, 2018, 10:03:22 PM
 #18

Alright. After testing it for few days. Here`s the result.

You`re right. I have to add it to the cron job because bot just stops sometimes. But I realized that sometimes the bot just stops however it doesn`t go back to command line. It just freezes. So can you add a line in the code where it will restart the script if it just stops or if it just sees a specific line in on the terminal while running?
Like
If "LINE LINE LINE" appears on the terminal page while running
Stop the script, go back to the command line and restart the script again with the same command it did before

Farklı Borsaların Anlık Bitcoin, Litecoin, Ethereum ve Bitcoin Cash Kurunu Takip Edebilirsiniz.
BTCkur.com
Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
March 09, 2018, 02:18:44 PM
 #19

Instead of keeping a terminal open, send it to background and redirect the output into a logfile.

I would try something like this:
Code:
#!/usr/bin/bash

if [[ ! $(pgrep -f "rtgferfd.py") ]]; then
echo "Bot down"
python rtgferfd.py --solid --r --fr --quantity 35 --wait_time 34 > /var/log/rtgferfd.log 2>&1 &
fi

if [ "$(grep -c "LINE LINE LINE" /var/log/rtgferfd.log)" -gt "0" ]; then
echo "Bot died"
pkill -f "rtgferfd.py"
python rtgferfd.py --solid --r --fr --quantity 35 --wait_time 34 > /var/log/rtgferfd.log 2>&1 &
fi

But really, fix the bugs in the bot instead of trying to bandaid them with workarounds.

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
BTC Turkiye (OP)
Sr. Member
****
Offline Offline

Activity: 472
Merit: 254


Anlik Coin Fiyatlari BTCkur.com


View Profile WWW
March 10, 2018, 07:49:59 AM
 #20

Ok I`m having a problem now. Below script I got it saved in /home/kullanici/restart_script.sh

Code:
#!/usr/bin/bash
if [[ ! $(pgrep -f "python /home/kullanici/bot.py --wait_time 0.2 --stop_loss 0") ]]; then
    echo "Bot down"
    python /home/kullanici/bot.py --wait_time 0.2 --stop_loss 0 &
fi

So when I go to terminal and write bash /home/kullanici/restart_script.sh it runs perfectly. But when I put that file in /etc/cron.hourly/ it doesn`t run it. I also went to terminal and wrote bash /etc/cron.hourly/restart_script.sh and it doesn`t run like that either. It says

Code:
root@debian:/etc/cron.hourly# Traceback (most recent call last):
  File "/home/kullanici/bot.py", line 9, in <module>
    from Trading import Trading
ImportError: No module named Trading

Farklı Borsaların Anlık Bitcoin, Litecoin, Ethereum ve Bitcoin Cash Kurunu Takip Edebilirsiniz.
BTCkur.com
Pages: [1] 2 »  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!