0

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.

Sheldon
  • 169
  • 1
  • 2
  • 16
  • there is another question then answers yours! https://stackoverflow.com/questions/1603109/how-to-make-a-python-script-run-like-a-service-or-daemon-in-linux – Pedro Dalcolli Mar 08 '20 at 15:19
  • Show the `.service` file and the output of `sudo systemctl status .service` – stovfl Mar 08 '20 at 16:25

0 Answers0