Sunday, June 5, 2022

[SOLVED] python script fails to work when using nohup

Issue

I am trying to execute a python script using the following syntax

[root@staging bucket-sync]# nohup python main.py

This script runs the following command inside:

import os
logging_directory = '/var/log/s3bucket'
os.system(f'mkdir {logging_directory}')

but in nohup.out I get the following error:

  File "main.py", line 20
    os.system(f'mkdir {logging_directory}')
                                         ^
SyntaxError: invalid syntax
  File "main.py", line 20
    os.system(f'mkdir {logging_directory}')
                                         ^

However, when I run the script without nohup, it works perfectly fine.

Is there a relationship between nohup and python and if so, can it be configured somewhere?


Solution

Hazarding a guess here, but if you specify python3 when you run this it should work.

You get this error from python 2, since it doesn't understand f'' strings.



Answered By - doctorlove
Answer Checked By - Marilyn (WPSolving Volunteer)