I’ve got a shell script as follows
ss.sh
#!/bin/bash opFile="custom.data" sourceFile="TestOutput" ./fc app test > $sourceFile grep -oP '[0-9.]+(?=%)|[0-9.]+(?=[A-Z]+ of)' "$sourceFile" | tr 'n' ',' > $opFile sed -i 's/,$//' $opFile
The requirement is that I need to use this script with the watch command. And I’d like to make this into a systemctl service. I did it as so.
sc.sh
#!/bin/bash watch -n 60 /root/ss.sh
And in my /etc/systemd/system,
log_info.service
[Unit] Description="Test Desc" After=network.target [Service] ExecStart=/root/sc.sh Type=simple [Install] WantedBy=default.target
When I run systemctl start log_info.service, It runs but not continuously the way I’d like it to.
On running sytemctl status log_info.service,
info_log.service - "Test Desc" Loaded: loaded (/etc/systemd/system/info_log.service; disabled; vendor preset: enabled) Active: failed (Result: exit-code) since Mon 2016-09-12 08:17:02 UTC; 2min 18s ago Process: 35555 ExecStart=/root/sc.sh (code=exited, status=1/FAILURE) Main PID: 35555 (code=exited, status=1/FAILURE) Sep 12 08:17:02 mo-b428aa6b4 systemd[1]: Started "Test Desc". Sep 12 08:17:02 mo-b428aa6b4 sc.sh[35654]: Error opening terminal: unknown. Sep 12 08:17:02 mo-b428aa6b4 systemd[1]: info_log.service: Main process exited, code=exited, status=1/FAILURE Sep 12 08:17:02 mo-b428aa6b4 systemd[1]: info_log.service: Unit entered failed state. Sep 12 08:17:02 mo-b428aa6b4 systemd[1]: info_log.service: Failed with result 'exit-code'.
Any ideas as to why it’s not running right? Any help would be appreciated!
Advertisement
Answer
So the reason I learnt (from superuser) for this failing was exactly what was in my error console, i.e,
Error opening terminal: unknown
Watch can only be executed from the terminal because it requires access to a terminal, while services don’t have that access.
A possible alternative to watch could be using a command that doesn’t require the terminal, like screen
or tmux
. Or, another alternative which worked for me, that was suggested by grawity on superuser, was
# foo.timer [Unit] Description=Do whatever [Timer] OnActiveSec=60 OnUnitActiveSec=60 [Install] WantedBy=timers.target
The logic behind this was that, the need was to run the script every 60 seconds, not to use watch. Hence, grawity suggested that I use a timer unit file that calls the service file every 60 seconds instead. If the service unit was a different name from the timer unit, [Timer] Unit=
can be used.
Hope this helped you and +1 to grawity and Eric Renouf from superuser for the answers!