always running python script

Solutions on MaxInterview for always running python script by the best coders in the world

showing results for - "always running python script"
Valentin
28 Jan 2017
1# in python, you should make a while(True) over the script
2flag = True
3while(flag):
4  # Your awesome script goes here
5	print("I'm running forever")
6    # flag is there to have an exit option if needed
7    flag = flase if statement == True else True
8
9    
10 
11""" Now we need to make changes in system level """  
12# In Windows #
13# Use built-in program "Task-Scheduler"
14- Create Task 
15- In 'Triggers' tab add new trigger and selecet 'At startup'
16- In 'Actions' tab add new action and write the command as you would write it in cmd (use absoulute path)
17press OK and you DONE!
18
19# In Ubuntu #
20Create file 'foo.service' inside '/etc/systemmd/system'
21and write inside 
22"""
23[Unit]
24After=network.target
25Description=bot
26
27[Service]
28Type=simple
29
30ExecStart=/bin/bash -c 'python3 /home/ubuntu/Path/To/Directory/main.py'
31
32Restart=always
33RestartSec=5s
34
35[Install]
36WantedBy=multi-user.target
37"""
38
39then in terminal run
40sudo systemctl enable foo.service
41sudo systemctl start foo.service 
42Done!
43
44
45
46