If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Need to make daemon on ubuntu hosting

Started by lizatailor23, Mar 09, 2023, 04:00 AM

Previous topic - Next topic

lizatailor23Topic starter

I developed a Python-based telegram bot and deployed it on an Ubuntu server. After installing all the necessary dependencies, I tested the script manually and verified that everything is functioning properly. However, I encountered difficulties in setting up a stable daemon that would allow the script to run without my intervention through remote access.

Despite numerous attempts, following tutorials or suggested solutions did not yield the desired outcome. Each time, I encountered some sort of error or encountered issues that prevented the successful implementation of a fully autonomous system.

If you have any suggestions or alternative approaches to resolve this matter, I would greatly appreciate your input.
  •  


langergrp

To initiate the process and track it, you can use the following command:

nohup .../path/to/main.py > out.log &

To locate the running process, you can execute the following command:

ps ax | grep main.py

This will help you monitor and manage the execution of your script effectively. Additionally, ensuring that the process is running smoothly is crucial for uninterrupted operation.
  •  

lexxora

To install the python-daemon library, you can use the following command:

pip3 install python-daemon

After importing the necessary modules, you can create a daemon by utilizing the DaemonContext class provided by the python-daemon library. This allows you to encapsulate your main program's functionality within the daemon's context.

Within the daemon context, you can call the appropriate function, such as 'do_main_program()', which represents the main function of the daemon. This ensures that the daemon executes the desired functionality while running in the background.

Implementing a daemon can provide several advantages, such as automatic restart on failure and the ability to run the program without manual intervention. This enables a smoother and more efficient execution of your program's tasks.
  •  

myaryan

Running a script or application as a daemon can be tricky, especially when it comes to handling errors, logging, and ensuring that the process remains running even after system reboots or crashes.

Here are some suggestions and alternative approaches you can consider:

1. Systemd service: Systemd is the modern init system used in most Linux distributions, including Ubuntu. It provides a straightforward way to manage system services, including your Python script. You can create a systemd service file that defines how your script should be run, restarted, and monitored. This approach ensures that your script runs as a background process and automatically restarts if it crashes or if the system reboots.

2. Supervisor: Supervisor is a client/server system that allows you to monitor and control various processes on Unix-like operating systems. It can automatically restart your Python script if it crashes or stops running for any reason. Supervisor also provides convenient features like logging and process grouping, making it easier to manage multiple processes on the same server.

3. Docker container: Another approach is to run your Python script inside a Docker container. Docker provides a consistent and isolated environment for your application, making it easier to manage dependencies and ensuring that your script runs the same way across different environments. You can create a Docker image with your Python script and all its dependencies, and then run it as a container on your Ubuntu server. Docker also provides built-in tools for managing and monitoring containers, which can help ensure that your bot runs continuously.

4. Python script with a loop: If you prefer a more straightforward approach, you can modify your Python script to run in an infinite loop. Within this loop, you can add error handling and logging mechanisms to ensure that the script restarts automatically if it encounters any issues. Additionally, you can use tools like `screen` or `tmux` to run your script in a persistent session, allowing you to detach from the session and reconnect later without interrupting the script's execution.

5. Process monitoring tools: There are various process monitoring tools available, such as Monit or Upstart, which can be used to monitor your Python script and automatically restart it if it stops running. These tools provide additional features like resource monitoring, alerting, and logging, which can be useful for maintaining a stable and reliable system.

Regardless of the approach you choose, it's essential to implement proper logging mechanisms within your Python script to help troubleshoot any issues that may arise. Additionally, consider setting up monitoring and alerting systems to receive notifications if your bot encounters any problems or stops running unexpectedly.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...