Bash – How to Execute a Program at Login

arch linuxbashprofileraspberry pi

I'm running Arch-Linux ARM on a Raspberry Pi.

My project goal is to have a barcode scanner attached to the Pi that forwards the scanned messages to a server in a LAN.

To harden the system against power outages etc. and minimize required user interaction, I wish to set it up so that a certain user is logged in automatically at startup (done and works) and a specific program is to be run at startup (done and works but not as intended).

I've done this so far by adding

exec mono scannerSoftware.exe 127.0.0.1 1234 randomString

to /home/certainUser/.bash_profile so it gets executed only once even when I'm switching back and forth to root (in contrast to bashrc).

The particular problem I am having:
When I launch the system, it works as intended (login as certainUser, starting scannerSoftware.exe); BUT if I want to switch users and hit Ctrl+C, it restarts the login process, logging certainUser in again and running the program again => back to where I came from.

My guess as to why this happens is that the sys detects "login not complete, better start it again".

I could circumvent this by spamming Ctrl+C right around the login so that I practically interrupted it. Of course, this is not good practice. Suffice it to say it is not practical at all.

My question:
How can I set it up so the program gets executed AFTER login is complete so that Ctrl+C'ing out brings me to a normal bash prompt as certainUser instead of beginning the endless login-cycle?

EDIT:
I'm not using any graphical interface or such, only commandline (bash on tty), because when done the system won't even have a display.

Best Answer

The answers depends on your requirements.

You might only want it to start after a particular user logs-in, alternatively you may want it to start when during reboot (a service). ?

start on login

When you start a program with exec it replaces the process that is used to start it.

  • So currently, your login bash is replaced by your scanner and ctrl-c sends interrupt signal, it stops, and you are then logged out.
  • instead of execing you could simply start the program, it runs in foreground (you don't get the prompt back). ctrl-c has the same effect as before, stops the scanner, but you will be back at the login bash prompt.
  • You can start the process in the background, by appending & to the command line, this creates a new process, still displays output on your terminal, but you have a prompt from your login shell as well, so output from the background process could interfere with your foreground shell. You can get over this by redirecting stdout and stderr to a file (even /dev/null if it's not wanted), thus avoiding the interference.

When you logout it's still running in background, it will get a hangup signal (but can catch that), so still processing in the background. If it was attached to your terminal it will become detached (output discarded, unless you had already catered for redirection).

When you login again, then a new scanner will be started, unless you check whether there is already one running, and it's easy to do that.

start during reboot

The advantage with this method is you do not need to login to get the process going, the disadvantage is that it is running all the time, you may not want that.

There are 3 main system start-up mechanisms, init, upstart and systemd, each being different. Traditionally /etc/rc.local would be the place to add machine specific system processes.

As WilQu mentions, Arch uses systemd, and he has provided a recipe for this, so I won't repeat.

advice

Write a script to start your process, even if it's only one line, the chances are that you will want to do more than just execute it, so you might as well write the short script now. (check if it's running, store the pid, use a different random string based on date ? who knows ...).

This allows you to develop and test the script, running it by hand and ensuring it woks as desired.

Then you can add you script, where you want (by hand, login profile, system startup).

Related Question