set Requisite in systemd service,but it can not work - systemd

ubuntu:20.04
flask_app.service
[Unit]
Description=python web service(using gunicorn WSGI)
After=syslog.target network.target
Requisite=mysql.service
[Service]
Type=simple
PIDFile=/data/project/xhxf/log/flask_app.pid
WorkingDirectory=/data/project/xhxf/
ExecStart=/usr/bin/gunicorn -b localhost:7071 app:app
[Install]
WantedBy=multi-user.target
status of flask_app.service
status of mysql.service
when I start flask_app.service and I don't start mysql.service,mysql.service is still inactive, but flask_app.service is active.
if I also add After=mysql.service in the unit module,then I start flask_app.service and I don't start mysql.service,both mysql.service and flask_app.service are inactive. Why? Requisite= Can't it be used alone?Does it have to be used with After= to make it work
I add After=mysql.service in the unit module,then I start flask_app.service and I don't start mysql.service,both mysql.service and flask_app.service are inactive

Related

Systemctl service with variables show empty

When VM is created, a systemctl service is installed (script1.sh) runs, then later (script2.sh) runs, but variables in service are not updated.
Background
I have a systemctl service which is defined when VM is created. (script1.sh)
Later I collect variables values that I would pass to this script. (script2.sh)
script1.sh
cat <<-EOH > /lib/systemd/system/my_service.service
[Unit]
Description=My Service
StartLimitIntervalSec=600
[Service]
Type=simple
ExecStart=/bin/bash -c '/opt/bin/agent --host=localhost:${PORT} --debug=${_DEBUG}'
Restart=always
[Install]
WantedBy=multi-user.target
EOH
script2.sh
PORT=$(get_random_port)
_DEBUG=$(get_debug_value)
sudo systemctl enable my_service.service
sudo systemctl restart my_service.service
when VM is created, service is installed (script1.sh) runs, then later (script2.sh) runs, but I don't see PORT or _DEBUG getting updated.
Troubleshooting done:
pass set -a
print env and I can see PORT & _DEBUG with correct value
pass Environment to systemctl service.
[Unit]
Description=My Service
StartLimitIntervalSec=600
[Service]
Type=simple
Environment="PORT=${PORT}"
Environment="_DEBUG=${_DEBUG}"
ExecStart=/bin/bash -c '/opt/bin/agent --host=localhost:${PORT} --debug=${_DEBUG}'
Restart=always
[Install]
WantedBy=multi-user.target
I tried to overwrite the service and that works, but does not seem to be a solid solution.

Custom port in Systemd scrip

I'm trying to set a custom port in my Systemd script, here is my .service file:
[Unit]
Description=discord-stock-ticker
Wants=basic.target
After=basic.target network.target
Before=sshd.service
[Service]
SyslogIdentifier=discord-stock-ticker
StandardOutput=syslog
StandardError=syslog
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/opt/discord-stock-ticker/discord-stock-ticker -port 42069
Restart=always
[Install]
WantedBy=multi-user.target
I've tried a bunch of different option like --PORT=xxx or --server.port=xxx, but it still run 8080 port.
Did you run systemctl daemon-reload after editing the service file? You need to in order to "commit" the changes so to speak.

Run Airflow HA Scheduler as systemd services

I want to run 2 airflow schedulers for which i created a systemd service file ending with #.service.
Now if I try to run the service like
sudo systemctl start airflow-scheduler#{1..2}
Only one of the schedulers manages to run, while the other one runs into an error which says
sqlalchemy.exc.DatabaseError: (mysql.connector.errors.DatabaseError) 3572 (HY000): Statement aborted because lock(s) could not be acquired immediately and NOWAIT is set.
My service file looks like this:
[Unit]
Description=Airflow scheduler daemon
After=network.target postgresql.service mysql.service redis.service rabbitmq-server.service
Wants=postgresql.service mysql.service redis.service rabbitmq-server.service
[Service]
EnvironmentFile=/etc/sysconfig/airflow
User=myuser
Group=myuser
Type=simple
ExecStart=/usr/local/bin/airflow scheduler
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target
The problem was with mysql connector python. I used mysqldb instead in the airflow config file and it works fine now.

systemd service not being triggered by its timer unit

Here is my service unit definition
[Unit]
Description=My Service
[Service]
ExecStart=/bin/bash -lc /usr/local/bin//myservice
# ExecStop=/bin/kill -15 $MAINPID
EnvironmentFile=/etc/myservice/config
User=myuser
Group=mygroup
and its timer unit file
[Unit]
Description=Timer for myservice
[Timer]
Unit=myservice.service
OnCalendar=*-*-* 10:33:00
[Install]
WantedBy=timers.target
I have tentatively set the OnCalendar to *-*-* 10:33:00 (followed by a sudo systemctl daemon-reload) but as a I was watching my machine, I didn't see the service firing. I had also set it for 5AM but this morning I saw no evidence of execution
When I perform a manual sudo systemctl start myservice it works as expected.
What might be preventing the service from executing according to its timer schedule?
You did not start the timer.
sudo systemctl start myservice.timer

Instruct to execute an unit after completing another unit successfully

Friends.
I use cloud-config to install and configure DCOS cluster.
Normally "agentinstall.service" service takes 5 minutes to complete.
Is it possible to instruct to systemd to execute "agentconfigure.service" ONLY AFTER "agentinstall.service" completed?
#cloud-config
coreos:
units:
- name: "agentinstall.service"
command: "start"
content: |
[Unit]
Description=agent_setup
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/tmp
ExecStartPre=/bin/curl -o /tmp/dcos_install.sh http://bootstapnode-0.dev.myztro.internal:9090/dcos_install.sh
ExecStartPre=/bin/chmod 755 dcos_install.sh
ExecStart=/bin/bash dcos_install.sh slave
[Install]
WantedBy=multi-user.target
- name: "agentconfigure.service"
command: "start"
content: |
[Unit]
Description=agent_config
After=agentinstall.service
[Service]
Type=simple
User=root
WorkingDirectory=/opt/mesosphere/etc/
ExecStartPre=/bin/echo "MESOS_ATTRIBUTES=cluster:uploader" >> /opt/mesosphere/etc/mesos-slave-common
ExecStartPre=/bin/rm -f /var/lib/mesos/slave/meta/slaves/latest
ExecStart=/usr/bin/systemctl restart dcos-mesos-slave
[Install]
WantedBy=multi-user.target
Thank you.
This is very much possible with systemd using After/Before keywords.
you can use something like below
In agentconfigure.service, provide below instruction
After=agentinstall.service
Usually After ensures that the dependent service is launched after launch of given service.
Since you mentioned that agentinstall.service takes 5 minutes to complete, so you have to add Type=notify in agentinstall.service, and from your application do a sd_notify() when your processing is done.
Based on this systemd shall start next service i.e agentconfigure.service
Read more about same here
Read about sd_notify() here

Resources