Failed to resolve unit specifiers in /var/bot/error-$(date+%Y_%m_%d_%I_%M_%p).log: Invalid slot - node.js

I want to run NodeJS code as service, but whenever I add output to log files I get error with syntax.
● bot.service - simple NodeJS bot
Loaded: bad-setting (Reason: Unit bot.service has a bad unit file setting.)
Active: inactive (dead)
Dec 21 17:33:36 ubuntu systemd[1]: /etc/systemd/system/bot.service:9: Failed to resolve unit specifiers in /var/bot/error-$(date+%Y_%m_%d_%I_%M_%p).log: Invalid slot
Dec 21 17:33:36 ubuntu systemd[1]: bot.service: Unit configuration has fatal error, unit will not be started.
bot.service:
[Unit]
Description=simple NodeJS bot
After=network-online.target
[Service]
Restart=on-failure
SyslogIdentifier=bot
WorkingDirectory=/home/ubuntu/bot
ExecStart=/home/ubuntu/.nvm/versions/node/v16.6.2/bin/node /home/ubuntu/bot/out/index.js >> /var/bot/bot.log 2> /var/bot/"error-$(date+%Y_%m_%d_%I_%M_%p).log"
# limit CPU and RAM quota for our service
# Not necessary, but i am running other servers, so i need it
CPUAccounting=true
MemoryAccounting=true
SyslogIdentifier=bot
[Install]
WantedBy=multi-user.target

The problem comes from the % sign. You need to escape it (%%) if you want the command to be accepted by systemd

Related

Why is rhel8 aws systemd service throws No such file or directory error even when file exists?

I have a systemd service define on rhel8
[Unit]
Description=Apache Kafka - ZooKeeper
Documentation=http://docs.confluent.io/
After=network.target
[Service]
Type=simple
EnvironmentFile=/app/bin/confluent/etc/kafka/zookenv.properties
User=kafka
Group=kafka
ExecStart=/app/bin/confluent/bin/zookeeper-server-start /app/bin/confluent/etc/kafka/zookeeper.properties
TimeoutStopSec=180
Restart=no
[Install]
WantedBy=multi-user.target
when i start this service i get the below error in journalctl
Jul 09 12:00:51 10.204.142.111 systemd[1]: confluent-zookeeper.service: Failed to load environment files: No such file or directory
Jul 09 12:00:51 10.204.142.111 systemd[1]: confluent-zookeeper.service: Failed to run 'start' task: No such file or directory
Jul 09 12:00:51 10.204.142.111 systemd[1]: confluent-zookeeper.service: Failed with result 'resources'.
the environment file exists in the path and so does the start script and properties files.
this is on RHEL8 aws and trying this for the first time.
the component starts up fine when i run the start script manually from command line.
Check that the path and file for
EnvironmentFile=/app/bin/confluent/etc/kafka/zookenv.properties
are correct
In my case, I had my service file like this
ExecStart=/app/bin/confluent/bin/zookeeper-server-start
EnvironmentFile=/app/bin/confluent/etc/kafka/zookenv.properties
I changed it to
EnvironmentFile=/app/bin/confluent/etc/kafka/zookenv.properties
ExecStart=/app/bin/confluent/bin/zookeeper-server-start
Then I run,
systemctl daemon-reload &&
systemctl restart service-name.service`
Initially, I was using `systemctl start service-name.service` and I guess that didn't make systemd read the environment files properly

Systemd service wont execute at boot

I've created my own Service with systemd. It is supposed to run a python script once at boot time. It sends an Email with the IP-Address and the Teamviewer id, this is why i have an delay in it, otherwise i get an error that the domain of the Mailserver cant be resolved. The Script should run in the background because of the 30 seconds delay.
The script is located in /usr/bin/glatv.py and is ecexuteable, the script run without an problem. The construct is runnning on an Raspberry Pi4 with Raspian Buster 2020-02-13
The Service is in /etc/systemd/system/ located, is executeable and enabled:
[Unit]
Description=My Own Service
[Service]
Type=oneshot
ExecStart=/usr/bin/glatv.py &
[Install]
WantedBy=reboot.target
But
systemctl start myservice
is working without a Problem
● glatvd.service - My Own Service
Loaded: loaded (/etc/systemd/system/glatvd.service; enabled; vendor preset: enabled)
Active: inactive (dead)
Apr 02 12:52:31 raspberrypi systemd[1]: Starting My Own Service...
Apr 02 12:53:02 raspberrypi systemd[1]: glatvd.service: Succeeded.
Apr 02 12:53:02 raspberrypi systemd[1]: Started My Own service.
after a reboot there is no call or log
Instead of having an arbitrary 30-second delay, add this to your service file:
After=network-online.target
Wants=network-online.target
You should try run this command to make your service enable to run after restart
systemctl enable myservice
and for log, I believe you must put this parameters into your service's config file
StandardOutput=/path/to/info/log/info_log.log
StandardError=/path/to/error/log/error_log.log
Anything I got this reference: How to redirect output of systemd service to a file

systemd service doesn't start silently. How to debug?

I wrote a program (called whisky) which I now want to startup when booting the machine (a Raspberry Pi with which I'm creating an autonomous boat). So I created the file /lib/systemd/system/whisky.service:
[Unit]
Description=Whisky Boat Program
After=network.target
StartLimitIntervalSec=0
[Service]
ExecStart=/home/pi/whisky/run
KillMode=process
IgnoreSIGPIPE=true
Restart=always
RestartSec=3
User=root
Type=simple
[Install]
WantedBy=multi-user.target
I verified the file is correctly formatted for systemd using systemd-analyze verify whisky.service.
When I now run sudo systemctl start whisky I get no output (suggesting no errors).
sudo systemctl status whisky gives me the following output though:
* whisky.service - Whisky Boat Program
Loaded: loaded (/lib/systemd/system/whisky.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Fri 2020-03-20 15:03:35 CET; 792ms ago
Process: 8621 ExecStart=/home/pi/whisky/run (code=exited, status=203/EXEC)
Main PID: 8621 (code=exited, status=203/EXEC)
Mar 20 15:03:35 raspberrypi systemd[1]: whisky.service: Unit entered failed state.
Mar 20 15:03:35 raspberrypi systemd[1]: whisky.service: Failed with result 'exit-code'.
The file /home/pi/whisky/run is actually a bash script which in turn starts the program. To check whether systemd even starts that bash script I added a first line to it: mkdir /home/pi/RUNNING_FROM_SYSTEMD. The dir RUNNING_FROM_SYSTEMD is not created though, so it seems systemd doesn't even try to run the file /home/pi/whisky/run.
Does anybody know what I'm doing wrong here?
Check your run script. There should be shebang. If not, add #!/bin/bash at the top of your script and give absolute path like ExecStart=/bin/bash /home/pi/whisky/run
The error message (code=exited, status=203/EXEC) is often seen when the script itself or its interpreter cannot be executed.
Other possible reasons maybe:
wrong path to script
script not executable
no shebang (first line) or wrong path in shebang
internal files in your script might be missing access permissions.

Systemd script fail

I want to run a script at system startup in a Debian 9 box. My script works when run standalone, but fails under systemd.
My script just copies a backup file from a remote server to the local machine:
#!/bin/sh
set -e
/usr/bin/sshpass -p "PASSWORD" /usr/bin/scp -p USER#10.0.0.2:ORIGINPATH/backupserver.zip DESTINATIONPATH/backupserver/
Just for privacy I replaced password, user, and paths above.
I wrote the following systemd service unit:
[Unit]
Description=backup script
[Service]
Type=oneshot
ExecStart=PATH/backup.sh
[Install]
WantedBy=default.target
Then I set permissions for the script:
chmod 744 PATH/backup.sh
And installed the service:
chmod 664 /etc/systemd/system/backup.service
systemctl daemon-reload
systemctl enable backup.service
When I reboot the script fails:
● backup.service - backup script
Loaded: loaded (/etc/systemd/system/backup.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2017-05-13 13:39:54 -03; 47min ago
Main PID: 591 (code=exited, status=1/FAILURE)
Result of journalctl -xe:
mai 16 23:34:27 rodrigo-acer systemd[1]: backup.service: Main process exited, code=exited, status=6/NOTCONFIGURED
mai 16 23:34:27 rodrigo-acer systemd[1]: Failed to start backup script.
mai 16 23:34:27 rodrigo-acer systemd[1]: backup.service: Unit entered failed state.
mai 16 23:34:27 rodrigo-acer systemd[1]: backup.service: Failed with result 'exit-code'.
What could be wrong?
Solved guys. There was 2 problems:
1 - I had to change the service unit file to make the service run only after network was up. The unit section was changed to:
[Unit]
Description = World server backup
Wants = network-online.target
After = network.target network-online.target
2 - The root user did not have the remote host added to the known host list, unlike the ordinary user I used to test the script.
Failed with result 'exit-code' you could try this on your last line:
# REQUIRED FOR SYSTEMD: 0 means clean no error
exit 0
You may also need to add:
Type=forking
to the systemd entry similar to: https://serverfault.com/questions/751030/systemd-ignores-return-code-while-starting-service
If your service or script does not fork add a & at the end to run it in the background, and exit with 0 fast. Otherwise it will be like a startup that times out and takes forever / seems like frozen service.

Systemd Zookeeper service failed

I've created the following systemd service script for running Zookeeper using Ansible:
[Unit]
Description=ZooKeeper
[Service]
User=root
Type=forking
User=zookeeper
Group=zookeeper
ExecStart=/tmp/zookeeper-3.4.9/bin/zkServer.sh start
ExecStop=/tmp/zookeeper-3.4.9/bin/zkServer.sh stop
TimeoutSec=300
[Install]
WantedBy=multi-user.target
But, while trying to run the script using sudo service zookeeper start I get the following error:
Nov 15 22:00:35 sharedservicesprovider systemd[1]: Starting ZooKeeper...
-- Subject: Unit zookeeper.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit zookeeper.service has begun starting up.
Nov 15 22:00:35 sharedservicesprovider systemd[15287]: zookeeper.service: Failed at step USER spawning /tmp/zookeeper-3.4.9/bin/zkServer.sh: No such process
-- Subject: Process /tmp/zookeeper-3.4.9/bin/zkServer.sh could not be executed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- The process /tmp/zookeeper-3.4.9/bin/zkServer.sh could not be executed and failed.
--
-- The error number returned by this process is 3.
Nov 15 22:00:35 sharedservicesprovider systemd[1]: zookeeper.service: Control process exited, code=exited status=217
Nov 15 22:00:35 sharedservicesprovider systemd[1]: Failed to start ZooKeeper.
-- Subject: Unit zookeeper.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
Any hint onto why I am unable to start Zookeeper properly since I'm quite new to system services?
Maybe so late... but use as ExecStart:
/tmp/zookeeper-3.4.9/bin/zkServer.sh start-foreground
[Unit]
Description=Zookeeper
After=syslog.target
[Service]
Environment=ZOO_LOG_DIR=/zookeeper/logs
SyslogIdentifier=zookeeper
TimoutStartSec=10min
Type=forking
User=zookeeper
Group=zookeeper
ExecStart=/zookeeper/bin/zkServer.sh start
ExecStop=/zookeeper/bin/zkServer.sh stop
[Install]
WantedBy=multi-user.target
ZOO_LOG_DIR is for permission require
this sounds like basics to check to me
first, make sure the file exists
ls -ltrah /tmp/zookeeper-3.4.9/bin/zkServer.sh
if it doesn't find where you extracted the zookeeper startup script. On my instance, it's /opt/kafka/bin/zookeeper-server-start.sh
Then verify that the user you have specified (you've specified two users, root and zookeeper. I'm assuming the last value is what's valid/desired) can access the script and has executable rights to it.
This would be done with chown and chmod commands, respectively.

Resources