0

I'm making a program in C that has the option (for the user) to launch automatically at Linux startup. I'm looking for a code snippet or something helpful. The way I want to do it, is programmatically if the user selected auto-startup, then (code I am looking for)

I saw many thing about cron jobs and /etc/rd.somethings, but it doesn't seems to be what I'm looking for.

alk
  • 69,737
  • 10
  • 105
  • 255
richerarc
  • 250
  • 1
  • 8
  • There's no standard way to do this that works across all Linux distributions. You would need to programmatically set up either a Systemd service or an Sysv init script or OpenRC script or whatever else depending on the system in use. – davmac Jun 29 '16 at 15:54
  • By your definition of the use case the code to start-up the program in question needs to be outside of the program itself. Also you might like to explain why using the cron-daemon could not help you? – alk Jun 29 '16 at 16:26
  • You could have the user optionally add the execution command to their .profile file. Possibly wrap in an if-block to check if it is already running and owned by that user. – FredK Jun 29 '16 at 16:37
  • There are at least three different stages at which processes can be launched automatically. 1) the system startup, or service level, which happens before any users are logged in. Normally, human-owned processes are not started yet, only general services. 2) GUI login, when a human user logs in to a graphical desktop environment. The programs that are started at this point is usually called *"the session"* or *"X session"*. If you always see e.g. an open browser when you logged in, that browser is in your session. 3) when logging in via a shell (terminal, SSH), applications can be run ... – Nominal Animal Jun 30 '16 at 07:10
  • ... as part of that user's shell startup scripts. (There are several options, ranging from running whenever a new shell is started, to only running if the shell is interactive, to only running if the shell is a login shell.) All of these are dependent on the Linux distribution (desktop environment, set of support services and applications used), so there is little sense in doing this "programmatically". I suspect (2) is the most likely one for you here; the best solution would be to tell the user to add your program to *"startup applications"* or the *"x session"*. – Nominal Animal Jun 30 '16 at 07:13

1 Answers1

1

if the user selected auto-startup, then (code I am looking for)

One way is to add a cron job to that user's crontab with special time specification @reboot.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271