Link to home
Start Free TrialLog in
Avatar of Nusrat Nuriyev
Nusrat NuriyevFlag for Azerbaijan

asked on

Adding custom startup script on fedora.

Performed actions:
Script used
/etc/init.d/mysript
Content of script

#!/bin/bash
# chkconfig: 345 90 10
# description: Description comes here....

### BEGIN INIT INFO
# Required-Start: $sendmail $mysqld $httpd
# Required-Stop: $sendmail $mysqld $httpd
# Default-Start: 345
# Default-Stop: 0126
### END INIT INFO
# Source function library
. /etc/rc.d/init.d/functions

mystart() {
   su -c '/myexecutable start' ejudge
}

mystop() {
   su -c '/myexecutable stop' ejudge
}

case "$1" in
    start)
    mystart
        ;;
    stop)
    mystop
        ;;
    restart)
    mystop
        mystart
    ;;
    *)
        echo "Usage: $0 start|stop|restart"
        exit 1
esac
exit 0

Open in new window


Commands performed:
chmod a+x myscript
chkconfig --add myscript
chkconfig --level 2345 myscript on

Open in new window


Testing workability:
ps ax | grep "myexecutable"
Does not give myexecutable as an output, so we conclude that application does not start.

Wishes:
The application must start on startup.

Thank you.
Avatar of Zephyr ICT
Zephyr ICT
Flag of Belgium image

Hi, what version is your Fedora? Are you using an older version with SysVinit or a newer one with Systemd, seeing you're using chkconfig it should be a system using SysVinit ...
Avatar of Nusrat Nuriyev

ASKER

Fedora 20.
Briefly, describe the differences between SysVinit and Systemd, and how to determine which one I belong
There is no immediate default way to check if you have either SysVinit or Systemd or Upstart for that matter, there is a way you can check, like this for example:

 ps -eaf|grep [s]ystemd

Open in new window


But since it is Fedora 20 it is using Systemd by default normally ...

To know some differences between Systemd and SysVinit, check this list

If you want to run a script at startup you can create a Systemd file in /usr/lib/systemd/system/<your_service_name>.service.

It should look something like this:

[Unit]
Description=<description>

[Service]
WorkingDirectory=<directory_of_script>
Type=forking
ExecStart=/bin/bash <the_absolute_path_to_script>
KillMode=process

[Install]
WantedBy=multi-user.target

Open in new window


Change the things between the brackets to comply with your setup.

Now tell Systemd to enable this service:

systemctl enable <your_service_name>.service

Open in new window


Reboot the system and see if the script runs like it should.
Check the messages log for info if it fails ( /var/log/messages).
Thank you for  systemd versus sysvinit information
Could we use command line arguments here?
ExecStart=/bin/bash <the_absolute_path_to_script>

Likewise

ExecStart=/bin/bash <the_absolute_path_to_script> <argument1> <argument2>

?
Another question is how to execute at shutdown which is binded to this service unit?


Actually, as you may see, I use single application which is started by
myexecutable start
and
terminate by
myexecutable stop

In sysvinit we have ability to do this, what about systemd?
If I understand your first question here, then yes you can, check out the Fedora info here

I'm not sure I understand your second question completely, but in systemd you can start stop a service like this:

systemctl start service.name
and
systemctl stop service.name
Then, systemd is not flexible enough, because I can't pass arguments to the executable, right?
Then, let's assume that we will solve the problem with good old sysinit?

Thank you.
What are your arguments?

If you look at this example you can give certain arguments:

[Unit]
Description=Avahi mDNS/DNS-SD Stack
Requires=avahi-daemon.socket

[Service]
Type=dbus
BusName=org.freedesktop.Avahi
ExecStart=/usr/sbin/avahi-daemon -s
ExecReload=/usr/sbin/avahi-daemon -r
NotifyAccess=main

[Install]
WantedBy=multi-user.target
Also=avahi-daemon.socket
Alias=dbus-org.freedesktop.Avahi.service

Open in new window

What is ExecReload?
It's used when your application/service supports reloading ... So as the name of it suggest.

You can leave this option blank if your service doesn't support it.
ok, what happened with the service state when system shutdown?

is it killed?
As far as I understand, the service file is also touched by the shutdown command, so you can normally just leave it as is, it will shutdown the service.

You can add a timeout under [service] if you want, for example:

TimeoutSec=300

Open in new window

System V init  scripts run inder both upstart (ubuntu, RHEL6) and systemd (Rhel7 etc)
No need to reinvent the wheel for every system.
It does not work.

When I manually start it /etc/init.d/myscript start
it works.
But it does not work on system startup.
It says something like "can't connect to mysql via socket."
ASKER CERTIFIED SOLUTION
Avatar of gheist
gheist
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe mariadb?
What does mean "name is being provided in EL7"? name of the package?
Thats the question.
No - sysV init scripts provide facilities like $local_fs or $network
So systemD does provide something similar, but you need to look into systemd descriptor what it is.