Start Program (with Text Output) at Startup

muhu

New Member
Joined
Oct 21, 2022
Messages
14
Reaction score
2
Credits
204
I have a custom command line program that I need to automatically execute after the system has (re)booted. The program

a) outputs text that I need to capture, either in a file or connecting to the process and see the output live.
b) the program writes some files to store captured data (multiple files) while the program is running.

I tried systemd but as I later found process running in a system daemon cannot capture the command line output in a file. I tried rc.local which can capture the output in a file with 2>&1 | tee output.txt but the program isn't able to write files. I also tried crontab but somehow it doesn't start at boot. Maybe CentOS 5 doesn't support it. What other options do I have?
 
Last edited:


CentOS 5 doesn't support systemd. You will need CentOS 7 or newer. If you want to use it.

I'm not sure what you mean by... "as I later found process running in a system daemon cannot capture the command line output in a file." Pretty much all processes that run the background are a daemon. ( I can think of at least 1 exception to that ).
Every systemd application that does logging writes to a file while it's running.
By the way, you can put the error/output redirects in the systemd startup line.

If you need it to automatically do something at startup, systemd will do this.

It is also possible with sysV init files (rc.local) but a little more difficult to me.

There is a way to do this with a special kind of crontab file, called a "firstboot" file.
The good news is.. this is pretty much just a bash script, so it can do anything you can do from the command line.
But it might be overkill for what you are trying to do.
 
CentOS 5 is more than 5 years beyond the EOL.
 
Correction, I am not on CentOS 5 but on CentOS 7.5.

When I used systemd I did try to redirect the output as I do on the command line, i.e. [program] 2>&1 | tee output.txt. This didn't work. Do I need a different kind of redirect with systemd?

rc.local seemed pretty simply. I just copied what I put into the command line. The program started and the output was redirected. However, the files that the program is supposed to write were not there. Any idea why they are not being written?

I will look into firstboot as a backup.

Update:
rc.local does actually write files. The problem is that I was looking for them in the directory where the program is. However, google told me that file generated by a program started with rc.local will belong to the root because the program runs under root. I then looked in the root directory and indeed, there were the files.
 
Last edited:

Members online


Top