0

I have started a python script in nohup. The script contains a finally block which will print the remaining items in the queue to print into a log file, if any Exception OR Keyboard interrupt occurs.

It is working fine as long as I'm in the same shell from where I executed the script. For eg: below steps are working as I have expected.

# Run the script in the background with nohup
nohup script_name.py > script_name.log 2>&1 &

# List the processes currently running in the background
jobs

# Bring back the process to foreground
fg 1

# Send Keyboard interrupt to stop the process by allowing to execute finally code block
Press CTRL+C

The script prints all the pending items in the queue to the "script_name.log" file.

But the problem is, once I exit the current shell from where I have started the "nohup" process, I can no longer bring that process back to foreground to send Keyboard interrupt.

At the same time, if I use kill PROCESS_ID command, it is not allowing the finally block to execute, so I'm also loosing the pending items in the queue.

Is there is any way I can terminate the process OR send a Keyboard interrupt to nohup process, still by allowing finally block to execute the code?

Thank you.

UPDATED: High level python code: It is actually a Python project with lot of directories and other scripts but let me give only the finally block part of the code here.

try:
    cc = crawler.MultiThreadedWebCrawler(max_workers)
    cc.run_web_crawler()
finally:
    cc.info(script_start_time)
    
    
    
# Inside another script, the definition of "info" method is,

def info(self, script_start_time):
    print('\n', self.crawl_queue.qsize(), ' URLs in crawl_queue are:\n')
    while self.crawl_queue.qsize() > 0:
        print(self.crawl_queue.qsize(), ' ', self.crawl_queue.get(), '\n')
    print("Script execution started at ", script_start_time)
    print("Script execution ended at ", datetime.now().strftime('%Y-%m-%d__%H_%M_%S'))
7
  • Could you please provide the code you're using? Commented Sep 1, 2022 at 23:54
  • 1
    Why do you exit the shell? Maybe just don't do that? Commented Sep 2, 2022 at 0:25
  • @KellyBundy, the reason we use "nohup" is to keep the long time consuming process (more than a month) running in the background without exiting, even after you exit the SSH session. In the current situation as well, the script is something which has already run more than 3 weeks and could still run until next couple of months. Commented Sep 2, 2022 at 0:34
  • 1
    Next time maybe use screen instead? Commented Sep 2, 2022 at 0:56
  • 1
    Looks like it, yes (just not sure this gnu one is the only one). I'm not that familiar with nohup, last used it maybe 20 years ago. But screen lets you reattach easily. I use it in ssh so I can close the connection at the end of the day and continue the next day, or continue after I somehow lost connection. As someone else said: "You should have used screen in the first place as it is a lot more flexible than nohup." Commented Sep 2, 2022 at 1:17

1 Answer 1

1

Use the value inside a txt file as a kill switch.

Have your program open a txt file every time it's finished it's 'jobs'. Check to see if the value extracted is a 1 or a 0. If it's 0 then continue. If it's 1 then exit the script and output the results.

KillOrStay = open("/root/KillOrStay.txt").readlines()[0]

if int(KillOrStay) == 1:
    # Output your results here
    exit()
else:
    pass
Sign up to request clarification or add additional context in comments.

Comments

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.