0

For example, I have the following script:

...
start(){
  echo "Starting..."   
  su - $ORA_OWNER -c "$ORA_HOME/bin/dbstart $ORA_HOME"       
  su - $ORA_OWNER -c "$ORA_HOME/bin/emctl start dbconsole"
  touch /var/lock/subsys/dbora 
}

stop(){
  echo "Stopping..."
  su - $ORA_OWNER -c "$ORA_HOME/bin/emctl stop dbconsole"
  su - $ORA_OWNER -c "$ORA_HOME/bin/dbshut $ORA_HOME"
  rm -f /var/lock/subsys/dbora
}

restart(){
  stop
  start
}

usage(){
  echo "usage: $0 {start|stop|restart}"
}
...

If I put this script in the directory /etc/init.d and after create a link in rc3.d with file name starting with S (start), How does Linux know that is needed to use function start()?

1
  • It would be helpful if you said what OS you use. Most of deskotp Linux distributions have switched to systemd. – Arkadiusz Drabczyk Mar 15 '20 at 20:03
3

If I put this script in the directory /etc/init.d and after create a link in rc3.d with file name starting with S (start), How does Linux know that is needed to use function start()?

I don't think it will. The function is internal to your script, and the script has to call it itself.

The other scaffolding surrounding that init script calls with an argument that tells what to do, start, stop, restart or such. The names of the links (K* and S*) tell what order to run the script and if they should be told to start or stop. Of course systemd does things a bit differently, but if I understand correctly, it can still support init scripts like that, and so has to pass them the correct argument when running them.

So then you see stuff like this in the init scripts:

do_start() {
    something here...
}
do_stop() {
    something else here...
}

case $1 in
    start)
        do_start;;
    stop)
        do_stop;;
    restart)
        do_stop;
        do_start;;
    *)
        usage;;
esac

The case in the bottom checks what the first argument is, and works based on that; that's the part missing from your snippet.

If you're writing a new init script, you should consider implementing is as a systemd service instead.

0

It (Linux) does not. Linux is a kernel. The Gnu/Linux operating system knows.

In particular the init process knows. init scripts have to have start, stop, restart. These methods have pre-defined meanings, and are called at the appropriate time. These methods are the interface to your script, you have to implement them.

1
  • This answer erroneously conflates init and rc too. – JdeBP Mar 16 '20 at 9:56
0

Because of the "S" you started the link with. When a link starts with "K" (from kill), it will run the stop function.

12
  • Who will run it? What is it? – Arkadiusz Drabczyk Mar 15 '20 at 20:02
  • As you can see in the other answer, the init process, used to be /sbin/init. Nowadays often crushed by systemd. – Gerard H. Pille Mar 15 '20 at 20:04
  • I know that. Your answer doesn't say that. Your answer says that it runs it. – Arkadiusz Drabczyk Mar 15 '20 at 20:05
  • Note the question is tagged sysvinit that is who runs it. – ctrl-alt-delor Mar 15 '20 at 20:07
  • IMO OP asks what part of OS runs this start() part of this script and they think that it's Linux whatever that means to them. – Arkadiusz Drabczyk Mar 15 '20 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.