How to Pipe Output to a File When Running as a Systemd Service? - linux

I'm having trouble piping the STDOUT & STDERR to a file when running a program as a systemd service. I've tried adding the following to the .service file:
ExecStart=/apppath/appname > /filepath/filename 2>&1
But this doesn't work. The output is ending up in /var/log/messages and is viewable using journalctl but I'd like a separate file.
I've also tried setting StdOutput=tty but can't find a way of redirecting this to a file.
Any help would be appreciated.

systemd.service(5) says:
ExecStart=
Commands with their arguments that are executed when this service is started.
So, systemd runs your /apppath/appname with args >, /filepath/filename, 2>&1
Try:
ExecStart=/bin/sh -c '/apppath/appname > /filepath/filename 2>&1'

Try:
ExecStart=/usr/bin/sh -c "/apppath/appname > /filepath/filename 2>&1"
ExecStart requires the first argument to be a binary (no exceptions), and doesn't allow pipes or redirection. Therefore, use ExecStart to start a shell within which you can do all the fancy things required.

Related

Running a process with the TTY detached

I'd like to run a linux console command from a terminal, preventing it from accessing the TTY by itself (which will, for example, happen often when the console command tries to request a password from the user - this should just fail). The closest I get to a solution is using this wrapper:
temp=`mktemp -d`
echo "$#" > $temp/run.sh
mkfifo $temp/out $temp/err
setsid sh -c "sh $temp/run.sh > $temp/out 2> $temp/err" &
cat $temp/err 1>&2 &
cat $temp/out
rm -f $temp/out $temp/err $temp/run.sh
rmdir $temp
This runs the command as expected without TTY access, but passing the stdout/stderr output through the FIFO pipes does not work for some reason. I end up with no output at all even though the process wrote to stdout or stderr.
Any ideas?
Well, thank you all for having a look. Turns out that the script already contained a working approach. It just contained a typo which caused it to fail. I corrected it in the question so it may serve for future reference.

Inconsistent stderr output between shell and systemd

I have a little script running at startup:
At first i was using crontab with #reboot tag, but, since some time i need to modify the script, i ended up looking at systemd for an easy restart of the script
my crontab config was:
#reboot sleep 10; cd /path/to/the/script/ && nohup ./script.py >> stdout.txt 2>> stderr.txt
and everything was working fine
I'm now using systemd with this config:
[Unit]
Description=Script
[Service]
WorkingDirectory=/path/to/the/script/
ExecStart=/path/to/the/script/script.py
StandardOutput=file:/path/to/the/script/stdout.txt
StandardError=file:/path/to/the/script/stderr.txt <----- Problem here
User=my_user
[Install]
WantedBy=graphical.target
the problem is:
before stderr was correctoly written to stderr.txt
now both stdout and stderr are written to stdout.txt, even if i specified a different file for stderr
There are also no errors looking at
journalctl -e -u python-coolosseo
BTW i tried both file: and append:

Conclusion of result of work of the Linux service?

Is automatically started as service at start of system a script. For example: myscript.service
Question - how to look through work (conclusion) of my script in real time. By analogy as though I have started the script from the terminal and I see a conclusion of work of a script.
service myscript status removes several lines and doesn't update a conclusion
journalctl -f -u myscript
Assuming you have used the systemd capabilities.
Either redirect> logs to the file and the tail -f command

Can we save the execution log when we run a command using PuTTY/Plink

I am using Plink to run a command on remote machine. In order to fully automate the process I
need to save the execution log somewhere. I am using a bat file:
C:\Ptty\plink.exe root#<IP> -pw <password> -m C:\Ptty\LaunchFile.txt
The C:\Ptty\LaunchFile.txt contains my command that i want to run.
./Launch.sh jobName=<job name> restart.mode=false
Is there a way to save the execution log so that I can monitor it later... ?
The plink is a console application. Actually that's probably it's only purpose. As such, its output can be redirected to a file as with any other command-line command/tool.
Following example redirects both standard and error output to a file output.log:
plink.exe -m script.txt username#example.com > output.log 2>&1
See also Redirect Windows cmd stdout and stderr to a single file.
This is the one of my way to log everything when I use putty.exe on Windows.

Linux: Daemon and Daemon output to be logged in a file

I am using the below command to run my python code as a daemon on the server.
nohup python mycode.py >> log.txt 2>&1 &
For some reason unknown to me only few lines are getting written to the file.
Thanks in advance
nohup itself writing output in nohup.out so no need to redirect output to log.txt,bydefault all output will be redirected to nohup.out

Resources