I am having service running as background service in EC2 instance. but it taking a long time to start a new one or restart a service ( refer to image: I reboot EC2, and a service restart at 9:00, but at the 11:32, a service print the first log ).
Here is my service config file in etc/systemd/system:
[Unit]
Description=file scanner ...
After=network.target
[Service]
User=root
Group=root
WorkingDirectory=/apps/file-scanner
ExecStart=/usr/bin/python3 main.py
StandardOutput=file:/var/log/file-scanner/logs.log
StandardError=file:/var/log/file-scanner/logs.log
[Install]
WantedBy=multi-user.target
Please help.
Suggesting to start your python script from command line to test how long it takes:
/usr/bin/python3 /apps/file-scanner/main.py
If it takes as long than you need to fix your /apps/file-scanner/main.py
and profile it to learn what takes long time. Use standard python3 profiling tools and put many logging message.
It is very possible your /apps/file-scanner/main.py
script is consuming most of the memory resources and cpu resources therefore your script takes very long to start.
Also learn to use top
command to monitor machine utilization at runtime.