0

('The First script' takes input from the user and 'the second script' notify the task.)

I have been trying to restart a python script using another one but i couldn't succeed it after trying to do a few methods. I developed a reminder, notify user when time previously set by the user has arrived, app works on Linux and it have 2 python script. First one is for taking input that given by the user to schedule a task. For example, "Call the boss at 12:30 pm". Then Linux is going to notify it at 12:30 pm. The other one is checking the inputs and notify them when the time comes.

In first script, i am trying to restart the other script when the user give a new task because the script needs to read the new task to notify it. Also I want to terminate the first script when it ran the second script. But the second script must still be working. In first script, I tried these commands to do that:

os.system(f"pkill -f {path2}")
os.system(f"python {path2}")

These aren't work.

Also I want to run the second script at the startup of my os.

Summary:

1- I wanna restart a python script using another one and the first one should be terminated when the second one is run.

2- I wanna run the second script at the startup of my os.

Repository about my reminder app is here.

10 Rep
  • 2,217
  • 7
  • 19
  • 33
sifirib
  • 13
  • 5
  • You answer is available in this https://stackoverflow.com/questions/3054740/terminate-a-python-script-from-another-python-script – abdulsaboor Aug 14 '20 at 22:28
  • to start at startup you could use `@reboot` in `cron`. But all this looks similar to program called `daemon` or `service` which has command `start`, `stop`, `restart` and they are automatically started after reboot. Maybe you should search information for this type of programs. – furas Aug 14 '20 at 22:30
  • I tried to use cron but it gave me this "python3: can't open file 'reminder2.py': [Errno 2] No such file or directory" when os is start. – sifirib Aug 14 '20 at 22:45
  • The command that i added in crontab is "@reboot sh /home/hrx/launcher_for_reminder.sh > /home/hrx/logsforreminder/cronlog 2>&1" – sifirib Aug 14 '20 at 22:57

1 Answers1

0

About 1 :

Assuming the name of the other script is 2.py (Changeable with the code below), this worked for me pretty well:

1.py:

import subprocess
import os
import time

OTHER_SCRIPT_NAME = "2.py"

process_outputs = subprocess.getoutput("ps aux | grep " + OTHER_SCRIPT_NAME) # Searching for the process running 2.py
wanted_process_info = process_outputs.split("\n")[0] # Getting the first line only
splitted_process_info = wanted_process_info.split(" ") # Splitting the string
splitted_process_info = [x for x in splitted_process_info if x != ''] # Removing empty items
pid = splitted_process_info[1] # PID is the secend item in the ps output
os.system("kill -9 " + str (pid)) # Killing the other process
exit()

time.sleep(1000) # Will not be called because exit() was called before

2.py:

import time

time.sleep(100)

About 2:

In linux, you can execute scripts on startup by writing it into the /etc/rc.local file

Just run your scripts from the rc.local file and you are good to go:

/etc/rc.local:

python '/path/to/your/scripts'
Dharman
  • 30,962
  • 25
  • 85
  • 135
davidalk
  • 87
  • 1
  • 1
  • 6
  • Why should i add "time.sleep(100)" to the 2.py and where should i place it? One more, if time.sleep(1000) will not be called, why do we add it? – sifirib Aug 14 '20 at 23:01
  • There isnt any file called rc.local in etc. – sifirib Aug 14 '20 at 23:11
  • I just created rc.local file under etc, made it executable, add the command with path of my app above the line exit 0 and restart the os but it didn't work. I'm sure. @Dharman – sifirib Aug 14 '20 at 23:27
  • All of the time.sleeps were to show that this method works. About /etc/rc.local - what was the command you were trying to run? I would advise you to just 'mkdir /home/[your_username]/tmp1' to test if the script really runs or not (after reset you should have tmp1 directory under your user's home directory). Should work. Also - Which distro of linux you are working with? – davidalk Aug 15 '20 at 08:31
  • There wasn't rc.local file under /etc and i created it and add these commands in it: (Then i made it executable) ` #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. exit 0 python '/home/hrx/Desktop/reminder/reminder.py' ` Its Linux Mint 20 – sifirib Aug 15 '20 at 09:52
  • Wow its my bad probably i add the command below exit 0 instead of above. – sifirib Aug 15 '20 at 10:05
  • 1.py that was answered by @davidalk works well but probably there is no way to restart 1.py with pid. I need to restart my script. That's why, I run it as a service on linux, but when i start the service the status command says: [here](justpaste.it/5zbk2). And paths of both python and python3 are [here](justpaste.it/3n365). – sifirib Aug 15 '20 at 13:31