Anconda env. python script autostart after reboot - anaconda

having a strange behaviour.
Setup:
Debian 11 server on Proxmox
added user mupsje (modded for sudo)
installed anaconda3 on mupsje and root.
created env discordbot.
directory: /home/mupsje/discordbot/
sh. file: on_startup.sh (chmodded)
#!/bin/bash
/home/mupsje/anaconda3/envs/discordbot/bin/python /home/mupsje/discordbot/bot.py
above line bot.py
#!/home/mupsje/anaconda3/envs/discordbot/bin/python
import blablabla
Now I want to start this env and the shell script when the server is reboot.
When I login with credentials mupsje or root and test the shell script.
(base) mupsje#debian:$ cd /home/mupsje/discordbot/
(base) mupsje#debian:$ cd ~/discordbot$ ./on_startup.sh
Python scripts runs.
Now i'm trying to ad this in systemd as a service
discordbot.service
[Unit]
Description=Startup Discordbot
[Service]
ExecStart=/home/mupsje/discordbot/on_startup.sh
[Install]
WantedBy=multi-user.target
when I start and status the systemctl service.
sudo systemctl start discordbot.service
sudo systemctl status discordbot.service
I get a error back and the python is not running.
* discordbot.service - Startup Discordbot
Loaded: loaded (/etc/systemd/system/discordbot.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2022-09-05 13:05:18 UTC; 9s ago
Process: 752 ExecStart=/home/mupsje/discordbot/on_startup.sh (code=exited, status=1/FAILURE)
Main PID: 752 (code=exited, status=1/FAILURE)
CPU: 355ms
Sep 05 13:05:18 debian on_startup.sh[753]: return future.result()
Sep 05 13:05:18 debian on_startup.sh[753]: File "/home/mupsje/anaconda3/envs/discordbot/lib/python3.10/site-packages/discord/client.py", line 817, in runner
Sep 05 13:05:18 debian on_startup.sh[753]: await self.start(token, reconnect=reconnect)
Sep 05 13:05:18 debian on_startup.sh[753]: File "/home/mupsje/anaconda3/envs/discordbot/lib/python3.10/site-packages/discord/client.py", line 745, in start
Sep 05 13:05:18 debian on_startup.sh[753]: await self.login(token)
Sep 05 13:05:18 debian on_startup.sh[753]: File "/home/mupsje/anaconda3/envs/discordbot/lib/python3.10/site-packages/discord/client.py", line 577, in login
Sep 05 13:05:18 debian on_startup.sh[753]: raise TypeError(f'expected token to be a str, received {token.__class__!r} instead')
Sep 05 13:05:18 debian on_startup.sh[753]: TypeError: expected token to be a str, received <class 'NoneType'> instead
Sep 05 13:05:18 debian systemd[1]: discordbot.service: Main process exited, code=exited, status=1/FAILURE
Sep 05 13:05:18 debian systemd[1]: discordbot.service: Failed with result 'exit-code'.
What I'm doing wrong?
regards
Mupsje

Found the solution myself.
I used crontab for this, with webmin "execute cron job as"
#reboot /home/mupsje/discordbot/on_startup.sh
then in on_startup.sh file.
#!/bin/bash
source ~/anaconda3/etc/profile.d/conda.sh
conda activate discordbot
echo $CONDA_DEFAULT_ENV
cd ~/discordbot/
python ppi_bot.py
conda deactivate
Works like a charm.
If you have better solutions, please let me know

Related

can't start minio in ubuntu due to "Variable MINIO_VOLUMES not set in /etc/default/minio";

I am installing latest minio on ubuntu 18.04 following the minio installation instruction from here.
after the installation, try to run it with sudo systemctl start minio.service
but it didn't work with message.
...skipping...
● minio.service - MinIO
Loaded: loaded (/etc/systemd/system/minio.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2022-12-08 17:03:45 CST; 2min 1s ago
Docs: https://docs.min.io
Process: 5072 ExecStart=/usr/local/bin/minio server $MINIO_OPTS $MINIO_VOLUMES (code=exited, status=1/FAILURE)
Process: 5050 ExecStartPre=/bin/bash -c if [ -z "${MINIO_VOLUMES}" ]; then echo "Variable MINIO_VOLUMES not set in /etc/default/minio"; exit 1; fi (code=exited, status=0/SUCCES
Main PID: 5072 (code=exited, status=1/FAILURE)
12月 08 17:03:45 nky systemd[1]: minio.service: Service hold-off time over, scheduling restart.
12月 08 17:03:45 nky systemd[1]: minio.service: Scheduled restart job, restart counter is at 5.
12月 08 17:03:45 nky systemd[1]: Stopped MinIO.
12月 08 17:03:45 nky systemd[1]: minio.service: Start request repeated too quickly.
12月 08 17:03:45 nky systemd[1]: minio.service: Failed with result 'exit-code'.
12月 08 17:03:45 nky systemd[1]: Failed to start MinIO.
it is noted something wrong with 'MINIO_VOLUMES', but I have set the variable in the /etc/default/minio
MINIO_ROOT_USER=myminioadmin
MINIO_ROOT_PASSWORD=minio-secret-key-change-me
# MINIO_VOLUMES sets the storage volume or path to use for the MinIO server.
MINIO_VOLUMES="/mnt/data"
what is wrong with my configuration?
There is nothing obvious wrong with your configuration but you did not post your service file. Almost always this is a permissions issue, you can change the systemd service user to root to test. Common issues after that are that the binary is not present in the location specified in the service file, or not executable.

Compute Engine start node server on instance startup

I am trying to run a discord bot in a node application in a free Compute Engine instance. I am struggling to make a script that actually starts the node app.
I created this script and added it as startup-script metadata from file:
cd code/movo-tron-2000 && npm start &
I checked that the script runs with sudo google_metadata_script_runner --script-type startup --debug, but when I restart the instance, the app doesn't start. Running sudo journalctl -u google-startup-scripts.service prints the following logs:
Apr 20 12:19:08 bot-vm systemd[1]: Starting Google Compute Engine Startup Scripts...
Apr 20 12:19:09 bot-vm startup-script[691]: INFO Starting startup scripts.
Apr 20 12:19:09 bot-vm startup-script[691]: INFO Found startup-script in metadata.
Apr 20 12:19:09 bot-vm startup-script[691]: INFO startup-script: /startup-od52epug/tmpjy_z4vue: line 1: cd: code/mo
Apr 20 12:19:09 bot-vm startup-script[691]: INFO startup-script: Return code 0.
Apr 20 12:19:09 bot-vm startup-script[691]: INFO Finished running startup scripts.
Apr 20 12:19:09 bot-vm systemd[1]: Started Google Compute Engine Startup Scripts.
I see that the script gets executed, but also gets terminated. The app listened for requests, so it shouldn't get terminated in order to run. I assume that the startup script gets run on the same thread as the google compute engine startup script so it gets terminated in order to continue the vm boot. What should I change in my startup script in order to start my app properly and not have it terminated by the instance?
Edit: I set up the following systemd service and script at their corresponding locations
Service:
[Unit]
Description=Start bot
[Service]
ExecStart=/home/me_adi_hf/code/movo-tron-2000/start.sh
[Install]
WantedBy=default.target
Script:
#!/bin/sh
date > /root/bot_report.txt
du -sh /home/ >> /root/bot_report.txt
But when running sudo systemd start bot.service and then checking it's status with sudo systemd status bot.service am getting this output, indicating Exec format error:
bot-start.service - Start bot
Loaded: loaded (/etc/systemd/system/bot-start.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2020-04-21 09:43:01 UTC; 9s ago
Process: 19303 ExecStart=/home/me_adi_hf/code/movo-tron-2000/start.sh (code=exited, status=203/EXEC)
Main PID: 19303 (code=exited, status=203/EXEC)
Apr 21 09:43:01 bot-vm systemd[1]: Started Start bot.
Apr 21 09:43:01 bot-vm systemd[19303]: bot-start.service: Failed at step EXEC spawning /home/me_adi_hf/code/movo-tron-2000/start.sh: Exec format error
Apr 21 09:43:01 bot-vm systemd[1]: bot-start.service: Main process exited, code=exited, status=203/EXEC
Apr 21 09:43:01 bot-vm systemd[1]: bot-start.service: Unit entered failed state.
Apr 21 09:43:01 bot-vm systemd[1]: bot-start.service: Failed with result 'exit-code'.
I am not sure what causes the error, since the service file syntax looks correct

The service has been enabled but failed. How can I make it run?

I use Linux Mint 19. I have created a simple script, "After_suspension", which will run three commands. I am trying to make it run when Mint wakes up from suspension.
matthew#matthew-pc:~$ cat /usr/local/bin/After_suspension
#!/bin/bash
pon dsl-provider
sudo service fancontrol start
/usr/bin/mailnag
matthew#matthew-pc:~$ file /usr/local/bin/After_suspension
/usr/local/bin/After_suspension: Bourne-Again shell script, ASCII text executable
"mailnag" is (text/x-python). I have created the following service file, which has been enabled but failed. How can I make it run? Should I use three separate service files to run the three commands?
matthew#matthew-pc:~$ cat /etc/systemd/system/After_suspension.service
[Unit]
After=suspend.target
[Service]
ExecStart=/usr/local/bin/After_suspension
[Install]
WantedBy=suspend.target
matthew#matthew-pc:~$ systemctl is-enabled After_suspension.service
enabled
matthew#matthew-pc:~$ systemctl is-active After_suspension.service
failed
matthew#matthew-pc:~$ systemctl status After_suspension.service
● After_suspension.service
Loaded: loaded (/etc/systemd/system/After_suspension.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2019-04-11 01:19:42 HKT; 3min 46s ago
Process: 11655 ExecStart=/usr/local/bin/After_suspension (code=exited, status=1/FAILURE)
Main PID: 11655 (code=exited, status=1/FAILURE)
Apr 11 01:19:11 matthew-pc After_suspension[11655]: File "/usr/lib/python2.7/dist-packages/dbus/bus.py", line 122, in __new__
Apr 11 01:19:11 matthew-pc After_suspension[11655]: bus = cls._new_for_bus(address_or_type, mainloop=mainloop)
Apr 11 01:19:11 matthew-pc After_suspension[11655]: dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NotSupported: Unable to autolaunch a dbu
Apr 11 01:19:07 matthew-pc systemd[1]: Started
After_suspension.service.
Apr 11 01:19:07 matthew-pc systemd[1]: After_suspension.service: Main process exited, code=exited, status=1/FAILURE
Apr 11 01:19:42 matthew-pc pppd[11657]: Timeout waiting for PADO packets
Apr 11 01:19:42 matthew-pc pppd[11657]: Unable to complete PPPoE Discovery
Apr 11 01:19:42 matthew-pc pppd[11657]: Terminating on signal 15
Apr 11 01:19:42 matthew-pc pppd[11657]: Exit.
Apr 11 01:19:42 matthew-pc systemd[1]: After_suspension.service: Failed with result 'exit-code'.
The following is the present "fancontrol.service".
matthew#matthew-pc:~$ cat /lib/systemd/system/fancontrol.service
[Unit]
Description=fan speed regulator
# Run pwmconfig to create this file.
ConditionPathExists=/etc/fancontrol
After=lm-sensors.service
Documentation=man:fancontrol(8) man:pwmconfig(8)
[Service]
ExecStartPre=/usr/sbin/fancontrol --check
ExecStart=/usr/sbin/fancontrol
PIDFile=/var/run/fancontrol.pid
[Install]
WantedBy=multi-user.target
Systemd runs system scripts as root, so you're trying to start GNOME disks as a root, in a terminal session with no X server active, that's why you probably get a connection refused message.
You probably want to configure that service to be an user service (you'll probably have to pass the DISPLAY variable, too), or just first try setting a DISPLAY variable in either your script or systemd service.

Fail2Ban: Service failed when log files symlink to another device

I am using a Raspberry Pi. To reduce I/O on my SD-Card I symlink all important log files to an external USB-mounted Harddrive.
Example:
ln -s /media/usb-device/logs/auth.log /var/log/auth.log
The logging works fine. But fail2ban seems not to like that. When I enable my ssh-monitoring in my /etc/fail2ban/jail.local file,
# [sshd]
enabled = true
bantime = 3600
fail2ban crash during executing this command systemctl restart fail2ban.service
I have tried to hardcode the path:
# logpath = %(sshd_log)s
logpath = /media/usb-devive/logs/auth.log
But fail2ban throws the same error:
fail2ban.service - Fail2Ban Service
Loaded: loaded (/lib/systemd/system/fail2ban.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2018-04-28 20:42:33 CEST; 45s ago
Docs: man:fail2ban(1)
Process: 3014 ExecStop=/usr/bin/fail2ban-client stop (code=exited, status=0/SUCCESS)
Process: 3045 ExecStart=/usr/bin/fail2ban-client -x start (code=exited, status=255)
Main PID: 658 (code=killed, signal=TERM)
Apr 28 20:42:33 raspberrypi systemd[1]: fail2ban.service: Service hold-off time over, scheduling restart.
Apr 28 20:42:33 raspberrypi systemd[1]: Stopped Fail2Ban Service.
Apr 28 20:42:33 raspberrypi systemd[1]: fail2ban.service: Start request repeated too quickly.
Apr 28 20:42:33 raspberrypi systemd[1]: Failed to start Fail2Ban Service.
Apr 28 20:42:33 raspberrypi systemd[1]: fail2ban.service: Unit entered failed state.
Apr 28 20:42:33 raspberrypi systemd[1]: fail2ban.service: Failed with result 'exit-code'.
Any ideas?
"devive" in the logpath is spelt incorrectly

Systemd service: blob data and return code

In Raspbian Stretch Lite I created the following systemd service:
[Unit]
Description=Autostart
After=multi-user.target
[Service]
Type=forking
ExecStart=/home/pi/autostart.sh
User=pi
Group=pi
[Install]
WantedBy=multi-user.target
Here the content of autostart.sh:
#!/bin/sh -ex
export TERM=linux
clear
mkdir -p /home/pi/logs
/home/pi/bin/./TestApp&
The script is actually executed (I added a debug echo to a file) but the application is not launched. It's a Qt5 console application, not a GUI one.
Trying to manually launch the script (i.e. ./autostart.sh) works as expected.
Instead, manually start the service leads to this output:
$ sudo systemctl start autostart.service
$ systemctl status autostart.service
● autostart.service - Autostart
Loaded: loaded (/lib/systemd/system/autostart.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2017-09-28 19:56:33 CEST; 9s ago
Process: 1351 ExecStart=/home/pi/autostart.sh (code=exited, status=0/SUCCESS)
Main PID: 1354 (code=exited, status=127)
Sep 28 19:56:33 localhost systemd[1]: Starting Autostart...
Sep 28 19:56:33 localhost autostart.sh[1351]: + export TERM=linux
Sep 28 19:56:33 localhost autostart.sh[1351]: + clear
Sep 28 19:56:33 localhost autostart.sh[1351]: [34B blob data]
Sep 28 19:56:33 localhost systemd[1]: Started Autostart.
Sep 28 19:56:33 localhost systemd[1]: autostart.service: Main process exited, code=exited, status=127/n/a
Sep 28 19:56:33 localhost systemd[1]: autostart.service: Unit entered failed state.
Sep 28 19:56:33 localhost systemd[1]: autostart.service: Failed with result 'exit-code'.
It's ok the mkdir command is not executed (the directory is already there), but I don't understand why application is not executed.
What could I do to get more information about what's happening?
First, running in the background is not the same as forking.
You can get rid of your autostart script and make a simpler systemd config file.
remove Type=forking. The default is Type=simple, which will handle running your app in the background for you.
Set Environment="TERM=linux" directly in the system configuration.
You can have multiple ExecStart= lines. Add a first one that is ExecStart=-/bin/mkdir -p /home/pi/logs, the extra "dash" there will allow the command to 'succeed' even if the directory has already been made.
Finally, run your app with ExecStart=/home/pi/bin/TestApp
Related man pages are man systemd.service, man systemd.exec, or look up any directive with man systemd.directives.

Resources