Running a console program at startup

If none of the more specific forums is the right place to ask

Running a console program at startup

Postby eldiener » 2018-03-15 20:35

I boot Debian and do not run any GUI system. I would like to start a console program in two different cases:

1) Before a login takes place but after Debian has fully booted and all network services have been started.
2) After a login takes place to be stared automatically after the login has completed. The login is always as the root user of the system.

The program uses the hardware on the system, most notably a modem which responds to AT commands, and the program uses openssl for security. All this needs ot have been started/initialized before the program starts.

For 1) above I have read a number of articles about starting a program like a service, such as http://xmodulo.com/how-to-automatically ... ebian.html, but they seem confusing to me despite my being a programmer. Most notably I do not understand how to insure that the program runs at runlevel 3, and I do not understand how to 'kill' the program at shutdown. From the console window, when running in the foreground, the program can be stopped by typing a particular sequence of letters in the window and hitting enter. But I do not know how to achieve this as part of a 'stop' action. Maybe there is some other way in Debian to stop the program and allow it to shutdown gracefully. The program is not a service.

For 2) above I imagine I can add the program to start at the end of the .profile for the root user. Is this the correct way to have the program start once the root user logs in ?
eldiener
 
Posts: 12
Joined: 2017-09-12 12:20

Re: Running a console program at startup

Postby Head_on_a_Stick » 2018-03-15 20:41

eldiener wrote:http://xmodulo.com/how-to-automatically-start-program-on-boot-in-debian.html

Debian uses systemd so you will have to learn that now:
Code: Select all
apropos systemd

^ The man pages are very thorough :)

EDIT: I would also use /root/.profile for your second scenario.
dbruce wrote:Ubuntu forums try to be like a coffee shop in Seattle. Debian forums strive for the charm and ambience of a skinhead bar in Bacau. We intend to keep it that way.
User avatar
Head_on_a_Stick
 
Posts: 8324
Joined: 2014-06-01 17:46
Location: /dev/chair


Re: Running a console program at startup

Postby eldiener » 2018-03-18 20:28

debiman wrote:https://wiki.debian.org/systemd
https://wiki.archlinux.org/index.php/Systemd


Your links are very nice but surely there must be some simple recipe for running a non-service console program at boot time without having to understand the details of systemd. If this is something that is hard or impossible to do in Debian then I would appreciate it if someone just said so.
eldiener
 
Posts: 12
Joined: 2017-09-12 12:20

Re: Running a console program at startup

Postby bw123 » 2018-03-18 20:44

It's not hard or impossible, but you do need to understand some details about how systemd works. There are plenty of examples all over the net, here's a basic tutorial http://www.linux-magazine.com/Issues/20 ... ls-Systemd

I didn't understand the description that the program starts before login, but you can stop it from console by typing into it's window? You say The program is not a service., but if it is running outside of a login, well, I kind of thought that is exactly what a service is?
User avatar
bw123
 
Posts: 3588
Joined: 2011-05-09 06:02
Location: TN_USA

Re: Running a console program at startup

Postby debiman » 2018-03-20 06:48

eldiener wrote:Your links are very nice but surely there must be some simple recipe for running a non-service console program at boot time without having to understand the details of systemd. If this is something that is hard or impossible to do in Debian then I would appreciate it if someone just said so.


[Removed by moderator]
User avatar
debiman
 
Posts: 3064
Joined: 2013-03-12 07:18

Re: Running a console program at startup

Postby GarryRicketson » 2018-03-20 16:42

eldiener wrote:If this is something that is hard or impossible to do in Debian then I would appreciate it if someone just said so.

It is hard, but not impossible. To clarify, I would find it difficult, because I do not understand the details of systemd, and I am to lazy to read the manuals.

Edited: removed quote, and comment
Last edited by GarryRicketson on 2018-04-03 18:45, edited 1 time in total.
User avatar
GarryRicketson
 
Posts: 5193
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Running a console program at startup

Postby bw123 » 2018-03-20 17:30

GarryRicketson wrote:
eldiener wrote:If this is something that is hard or impossible to do in Debian then I would appreciate it if someone just said so.

It is hard, but not impossible. To clarify, I would find it difficult, because I do not understand the details of systemd, and I am to lazy to read the manuals.


ah... come on Garry, it's not that bad. Actually, since systemd came along, most debian pkgs have an actual working config and an enabled and working service file when installed. You don't have to do much to get basic functionality besides apt install the pkg.

Anyway, that leads me to wonder why the OP needs to set this up, and what the "console program" mentioned is, and why it doesn't include a working service file, like most debian pkgs do?
User avatar
bw123
 
Posts: 3588
Joined: 2011-05-09 06:02
Location: TN_USA

Re: Running a console program at startup

Postby eldiener » 2018-04-02 21:54

debiman wrote:
eldiener wrote:Your links are very nice but surely there must be some simple recipe for running a non-service console program at boot time without having to understand the details of systemd. If this is something that is hard or impossible to do in Debian then I would appreciate it if someone just said so.

eldiener wrote:I have received a valid answer to my question. I don't like this answer, for whatever reason, so I'll keep on repeating my question until I get an answer I like. If that doesn't happen I will blame Linux for it.


I never said the second of your quotes. Why would you make something up like that !
eldiener
 
Posts: 12
Joined: 2017-09-12 12:20

Re: Running a console program at startup

Postby eldiener » 2018-04-02 21:57

GarryRicketson wrote:
eldiener wrote:If this is something that is hard or impossible to do in Debian then I would appreciate it if someone just said so.

It is hard, but not impossible. To clarify, I would find it difficult, because I do not understand the details of systemd, and I am to lazy to read the manuals.
eldiener wrote:
I have received a valid answer to my question. I don't like this answer, for whatever reason, so I'll keep on repeating my question until I get an answer I like. If that doesn't happen I will blame Linux for it.

That is fine, stick with Windows , or what ever other OS that works the way you want it to.
Bye


The second supposed quote was never made by me. Is there a way to complain about the person who made it up and then claimed it was by me ?
eldiener
 
Posts: 12
Joined: 2017-09-12 12:20

Re: Running a console program at startup

Postby eldiener » 2018-04-02 22:11

bw123 wrote:
GarryRicketson wrote:
eldiener wrote:If this is something that is hard or impossible to do in Debian then I would appreciate it if someone just said so.

It is hard, but not impossible. To clarify, I would find it difficult, because I do not understand the details of systemd, and I am to lazy to read the manuals.


ah... come on Garry, it's not that bad. Actually, since systemd came along, most debian pkgs have an actual working config and an enabled and working service file when installed. You don't have to do much to get basic functionality besides apt install the pkg.

Anyway, that leads me to wonder why the OP needs to set this up, and what the "console program" mentioned is, and why it doesn't include a working service file, like most debian pkgs do?


The program was initially designed to run as a console program when the /root user logged in on a Debian ARM SOM system. Then the request is that the program be running as part of Debian startup even before the /root user logged in, as it needs no user interaction other than a 'q' key to end the program, even though it does output informatory messages to its console window. That is what triggered my OP. Since this request did not involve running the program as a daemon, I thought there might be a fairly simple explanation to do this without having to understand how 'systemd' works. This is why I asked if this could be done without understanding 'systemd'. Someone else then quoted me falsely and some people reacted negatively to that false quote. It shows that even here people will fall for false information.

Now the latest request I have received is that the program run as a daemon where all output will go to a log file. I understand how to change the program as an old-style SysV daemon, but I believe that the new-style systemd daemon is what the Debian ARM system uses. So I will read up on 'systemd' with the links so graciously given me by others.
eldiener
 
Posts: 12
Joined: 2017-09-12 12:20

Re: Running a console program at startup

Postby reinob » 2018-04-03 13:13

eldiener wrote:The program was initially designed to run as a console program when the /root user logged in on a Debian ARM SOM system. Then the request is that the program be running as part of Debian startup even before the /root user logged in, as it needs no user interaction other than a 'q' key to end the program, even though it does output informatory messages to its console window. That is what triggered my OP. Since this request did not involve running the program as a daemon, I thought there might be a fairly simple explanation to do this without having to understand how 'systemd' works. This is why I asked if this could be done without understanding 'systemd'. Someone else then quoted me falsely and some people reacted negatively to that false quote. It shows that even here people will fall for false information.

Now the latest request I have received is that the program run as a daemon where all output will go to a log file. I understand how to change the program as an old-style SysV daemon, but I believe that the new-style systemd daemon is what the Debian ARM system uses. So I will read up on 'systemd' with the links so graciously given me by others.


Normally interactive programs are run when a user has logged in, and, more importantly, require a controlling terminal (so that they respond to the keyboard and can display on the screen). From what you've described, your program needs a controlling terminal.

Systemd to the rescue! What follows is untested and never actually tried (just theory)..

Create a systemd unit (text file) at /etc/systemd/system/, e.g. /etc/systemd/system/my-program.service
(it's a plain text file, the extension needs to be .service), with the following content:

Code: Select all
[Unit]
Description=My program
After=getty@tty2.service

[Service]
Type=oneshot
ExecStart=/path/to/your/program
StandardInput=tty
TTYPath=/dev/tty2
TTYReset=yes
TTYVHangup=yes

[Install]
WantedBy=default.target


This will (or should..) prepare a terminal on the second virtual terminal (ALT-F2) where your program will run interactively (as root, but you can change that too), independently of any users being logged in.

Once you've saved the file do "systemctl daemon-reload" then "systemctl enable my-program.service".
You can then either reboot or try "systemctl start my-program" and check if something has moved in VT2.

I'm not sure about the "After=" part but give it try and report back.
reinob
 
Posts: 574
Joined: 2014-06-30 11:42

Re: Running a console program at startup

Postby eldiener » 2018-04-03 16:16

reinob wrote:
eldiener wrote:The program was initially designed to run as a console program when the /root user logged in on a Debian ARM SOM system. Then the request is that the program be running as part of Debian startup even before the /root user logged in, as it needs no user interaction other than a 'q' key to end the program, even though it does output informatory messages to its console window. That is what triggered my OP. Since this request did not involve running the program as a daemon, I thought there might be a fairly simple explanation to do this without having to understand how 'systemd' works. This is why I asked if this could be done without understanding 'systemd'. Someone else then quoted me falsely and some people reacted negatively to that false quote. It shows that even here people will fall for false information.

Now the latest request I have received is that the program run as a daemon where all output will go to a log file. I understand how to change the program as an old-style SysV daemon, but I believe that the new-style systemd daemon is what the Debian ARM system uses. So I will read up on 'systemd' with the links so graciously given me by others.


Normally interactive programs are run when a user has logged in, and, more importantly, require a controlling terminal (so that they respond to the keyboard and can display on the screen). From what you've described, your program needs a controlling terminal.

Systemd to the rescue! What follows is untested and never actually tried (just theory)..

Create a systemd unit (text file) at /etc/systemd/system/, e.g. /etc/systemd/system/my-program.service
(it's a plain text file, the extension needs to be .service), with the following content:

Code: Select all
[Unit]
Description=My program
After=getty@tty2.service

[Service]
Type=oneshot
ExecStart=/path/to/your/program
StandardInput=tty
TTYPath=/dev/tty2
TTYReset=yes
TTYVHangup=yes

[Install]
WantedBy=default.target


This will (or should..) prepare a terminal on the second virtual terminal (ALT-F2) where your program will run interactively (as root, but you can change that too), independently of any users being logged in.

Once you've saved the file do "systemctl daemon-reload" then "systemctl enable my-program.service".
You can then either reboot or try "systemctl start my-program" and check if something has moved in VT2.

I'm not sure about the "After=" part but give it try and report back.


Thank you very much for the information. As a daemon I should not need a terminal if I am going to direct all output to a log file, should I ? Or did I misunderstand something in your explanation ? In other words any console output in the program I am going to replace with output to a log file and no console input will be attempted to end the program but it will just run in the background doing its task as long as the OS is running.
eldiener
 
Posts: 12
Joined: 2017-09-12 12:20

Re: Running a console program at startup

Postby reinob » 2018-04-03 16:41

eldiener wrote:Thank you very much for the information. As a daemon I should not need a terminal if I am going to direct all output to a log file, should I ? Or did I misunderstand something in your explanation ? In other words any console output in the program I am going to replace with output to a log file and no console input will be attempted to end the program but it will just run in the background doing its task as long as the OS is running.


Well, I don't know what exactly your program does and how. (I assume it is some kind of custom (non-debian) program that you can't modify..)

But if you say you can stop it by typing some command, it means that it is waiting for keyboard (stdin, i.e. tty) input. If a program expects a tty (stdin) and it doesn't have one (like a real daemon or "service") then it will most likely die as soon as it tries to open the tty for reading.

You can read https://www.freedesktop.org/software/sy ... .exec.html, and especially the section "Logging and Standard Input/Output". It's pretty cool actually. You could redirect the output (stdout) to a file (which you mentioned you want to do, so systemd does it for you for free), and the console input (stdin) could be even a Unix socket, so that when you want to stop it you could send the command via the socket. Really cool indeed!

Whether a VT is required or not, I don't really now. I think if you set Standard{Input,Output,Error} to something other than tty (e.g. a file or a socket) then the TTY* options are not relevant, so I presume that the program will run without any visible effect.

Give it a shot (or rather, a "oneshot" :) and report back.
reinob
 
Posts: 574
Joined: 2014-06-30 11:42

Re: Running a console program at startup

Postby pylkko » 2018-04-03 18:17

I don't have the time to retype it but I have posted on this forum a systemd service that kills a service upon shutdown and starting it when booting is pretty straight forward. You absolutely do not need a tty for it and you don't need a log for it either and actually systemd will not print a log unless you specify it in the .service. I have used these on robots that I have made running Debian that have no input methods/devices at all and no display.

edit:

see:
viewtopic.php?f=10&t=130577&hilit=+.service#p629835
User avatar
pylkko
 
Posts: 1357
Joined: 2014-11-06 19:02


Return to General Questions

Who is online

Users browsing this forum: 4D696B65 and 3 guests

fashionable