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.
Nusrat NuriyevAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zephyr ICTCloud ArchitectCommented:
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 ...
Nusrat NuriyevAuthor Commented:
Fedora 20.
Briefly, describe the differences between SysVinit and Systemd, and how to determine which one I belong
Zephyr ICTCloud ArchitectCommented:
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).
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Nusrat NuriyevAuthor Commented:
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>

?
Nusrat NuriyevAuthor Commented:
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?
Zephyr ICTCloud ArchitectCommented:
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
Nusrat NuriyevAuthor Commented:
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.
Zephyr ICTCloud ArchitectCommented:
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

Nusrat NuriyevAuthor Commented:
What is ExecReload?
Zephyr ICTCloud ArchitectCommented:
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.
Nusrat NuriyevAuthor Commented:
ok, what happened with the service state when system shutdown?

is it killed?
Zephyr ICTCloud ArchitectCommented:
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

gheistCommented:
System V init  scripts run inder both upstart (ubuntu, RHEL6) and systemd (Rhel7 etc)
No need to reinvent the wheel for every system.
Nusrat NuriyevAuthor Commented:
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."
gheistCommented:
It needs to require mysql/mysqld or something like that (i dont know what name is being provided in EL7)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nusrat NuriyevAuthor Commented:
Maybe mariadb?
What does mean "name is being provided in EL7"? name of the package?
gheistCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux Distributions

From novice to tech pro — start learning today.