0

I am trying to run my application directly from the linux kernel (without using cron or something similar). I've tried using ./init/init.c, but it runs too early:

$ dmesg 

...
[    0.605657] TEST!!!
...

My idea is to launch an application after successful user login, but I cannot find an appropriate function to use.

NoTrust
  • 153

2 Answers2

1

If you actually want it after the user logs in you can put it in your shell profile file. Most likely ~/.bash_profile.

You want to add a line, probably near the end, that simply calls the filename of your application as if you were running it from your terminal, for example: /home/username/bin/mypersonalscript.sh&. You may need to use the absolute path.

Also be sure to add the ampersand to the end. That forks the process to the background and without it you may not be able to get into your shell when you log in.

If it doesn't work, make sure your script is executable.

Johnyburd
  • 101
  • 2
    .bashrc is invoked every time the user starts a shell (e.g. typing :sh inside vi). If you want once per login then .bash_profile is a better place. (Or .profile if using ksh or sh as the shell; or .login if using csh or tcsh). – Stephen Harris Jul 16 '16 at 12:42
  • You're right of course. I'll update the answer. – Johnyburd Jul 16 '16 at 12:52
1

The job of the kernel is to run one process: init, which gets process ID 1. It's init's job to run other processes to provide system services and allow users to log in.

There are a few other cases where the Linux kernel will execute a process. For example, when the kernel detects new hardware on certain buses, it executes modprobe to load a driver as a module. Another example is that it's possible to configure a program to pipe core dumps through. I think all of these cases use the call_usermodehelper_xxx functions.

You'll note that there are only very few cases, and they're all triggered by a kernel event: hardware event or program termination. These are exceptional cases, reserved for low-level system functionality. The normal way to execute a process is that it's forked from a process that's forked from a process that's … that's forked from init.

The kernel doesn't even know about a “successful login”. Logging in is a high-level concept, well above the kernel. To run a process as root when a user logs in, add it to the PAM configuration, with the pam_exec module. To run a process as the user who logged in, add it to the user's shell startup file, typically ~/.profile.