How to make below python script merged in daemon service script mentioned below?
First Script: looking for files in a directory and move them to /tmp folder
Second Script: found in a link, that binds python script to linux daemon as a service.
I want to merge first script to daemon service script, so that till the service is running, my python script keeps checking for files, and move them to /tmp
folder.
first script:
from os import listdir
import shutil
from os.path import isfile, join
def my_func():
mypath = '/home/ansible/testdir/'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for file in onlyfiles:
shutil.move(mypath + file, "/tmp/" + file)
import time
while True:
my_func()
time.sleep(10)
Output:
Works as expected, looks for files in specified path and moves them to /tmp
directory as per time interval (time.sleep
) mentioned.
Second Script:
#!/usr/bin/python3
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("localhost", 9988))
s.listen(1)
while True:
conn, addr = s.accept()
data = conn.recv(1024)
conn.close()
my_function_that_handles_data(data)
Found this from https://tecadmin.net/setup-autorun-python-script-using-systemd/ and this works fine & I am able to start the service.
I have already tried including first script in daemon service script & called function after while loop, and when i start the service, i cant see files moving to /tmp
directory.