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.

4
  • You answer is available in this stackoverflow.com/questions/3054740/… Commented Aug 14, 2020 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. Commented Aug 14, 2020 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. Commented Aug 14, 2020 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" Commented Aug 14, 2020 at 22:57

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

7 Comments

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?
There isnt any file called rc.local in etc.
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
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?
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.