1

My pc runs a program when it boots. Sometimes the program stops working after a few hours, but it will work fine when it reboots. So, I want to schedule my pc to reboot every 3 hours to prevent the program to be stopped. I'm using Ubuntu 14.04.

edit: The program doesn't stop, it just doesn't work with the maximum power of the pc but I need it to work with full power.

Majidak
  • 47
  • 2
  • 9

1 Answers1

5

The following should work, but I am not sure about the command, since I am not familiar with gminer.

The script

Paste the text below in an empty file, save it as check_gminer.py, save it somewhere:

#!/usr/bin/python3

import subprocess
import getpass

curruser = getpass.getuser()
service = "gminer"

def createlist_runningprocs():
    processesb = subprocess.Popen(["ps", "-u", curruser], stdout=subprocess.PIPE)
    process_listb = (processesb.communicate()[0].decode("utf-8")).split("\n")
    return process_listb

def runsornot():
    runningprocs_list = createlist_runningprocs()
    if not application[:15] in str(runningprocs_list):
        subprocess.Popen(["sh", "m.sh"])
    else:
        pass

runsornot()

Editing cronfile

Add the following line to your cronfile (type crontab -e in a terminal):

*  *  *  *  *  python3 /path/to/script/check_gminer.py

The script looks every minute if the service runs ant restarts it if not. The question is if it works or not if your GPU has problems. We'll have to see.

Alternative procedure, if the above method is not sufficient for your situation

The alternative (reboot) needs administrator privileges. Therefore, if you need to run the reboot command by a cronjob, you need to edit /etc/crontab (sudo nano /etc/crontab), In which you can set by which user the command should be run (<> crontab -e).

Add the line:

0 */3 * * * root reboot

To the /etc/crontab and your computer will restart every three hours.

Jacob Vlijm
  • 83,767
  • Wow, that's nice. I've done what you said. I'll wait to see if this instruction works when the problem occurs. Thanks a lot. – Majidak Jun 12 '14 at 10:37
  • Nice one and easy re-usable too :-). Though I would suggest /etc/crontab over crontab -e :) – Rinzwind Jun 12 '14 at 11:29
  • @Rinzwind You are completely right of course, but I am not sure if gminer runs locally? – Jacob Vlijm Jun 12 '14 at 11:52
  • @JacobVlijm Your idea was clever, but as I edited the question, it doesn't solve the problem. Now, can you please guide me how to work with crontab to restart the PC every 3 hours? Actually, restarting has another advantage, the GPUs will rest for about 5 minutes :) – Majidak Jun 14 '14 at 17:38
  • @JacobVlijm Someone else, "Creator", answered the question. Thank you anyway for your time. – Majidak Jun 14 '14 at 17:52
  • 1
    @Majidak I am afraid don't understand much of the pretty crowded comment series beneath your question :), but anyway, it is * */3 * * * root reboot, important is that you add it to /etc/crontab (sudo nano /etc/crontab, not crontab -e) – Jacob Vlijm Jun 14 '14 at 18:12
  • @JacobVlijm Sorry about that. What's the difference between those two crontabs? Admin and non-admin user? – Majidak Jun 14 '14 at 18:14
  • @Majidak I added the info to my answer :) – Jacob Vlijm Jun 14 '14 at 18:25
  • I guess the first star should be changed to ,let's say, 1. Since, the pc will restart at any minute when it is dividable by 3. – Majidak Jun 14 '14 at 20:07
  • @Majidak Aaarrgh, excuse me, you are right of course, will change it. It should be 0 */3 * * * – Jacob Vlijm Jun 14 '14 at 20:46