It's sounding like you may really be trying to run your script as a service (i.e. the bot starts on boot and runs indefinitely), rather than a periodic job (which is what cron is traditionally used for). A better or more "correct" way to do this might be to just configure it as a systemd service; although it's one of those things were you have multiple different ways to achieve the same result, and it's ultimately up to you. Running a python script as a service is not difficult. You mostly just need to add a small service config file in /etc/systemd/system, e.g. /etc/systemd/system/discord-bot.service (it must be named *.service). There's a ton of configuration options for systemd services, but you really don't need much for a basic Python service. Here's a quick example:
The above assumes an executable Python script, but you can also just change ExecStart to "/usr/bin/python args…", if you prefer. See systemd.service(5) for more details, and /usr/lib/systemd/system/*.service for plenty of different examples. After creating your service file, just reload systemd, enable the service, and start it:
If desired, you can use systemd.journal.JournalHandler with Python's logging, to fully integrate your service logs with the system.
Systemd can be a bit intimidating at first, if you're not used to the inner workings of it, but you can kinda ignore 95% of it if you just need to add a simple service. You don't have to use it if you don't want to, I just thought I'd throw this out there and see if it helps.
Code:
[Unit]After=network-online.target nss-lookup.targetWants=network-online.target[Service]ExecStart=/usr/local/lib/discord-bot[Install]WantedBy=multi-user.target
Code:
sudo systemctl daemon-reloadsudo systemctl enable discord-bot.servicesudo systemctl start discord-bot.service
Systemd can be a bit intimidating at first, if you're not used to the inner workings of it, but you can kinda ignore 95% of it if you just need to add a simple service. You don't have to use it if you don't want to, I just thought I'd throw this out there and see if it helps.
Statistics: Posted by Murph9000 — Thu Aug 29, 2024 11:11 pm