systemctl testing shell scripts - linux

So I am trying arch linux, but I can't really understand how to make autostart scripts and such. I tried this (I know there are better ways to do it but it's just a test):
this is the service:
[Unit]
Description=testing purposes
[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/font
[Install]
WantedBy=multi-user.target
this is the script:
setfont Lat2-Terminus16
I chmod +x'd the script and stuff so I thought it wouldn't make any problems but it does not work unlike what I expected, so I am clearly doing something wrong. Where is the problem?
EDIT: Solved, I changed the ExecStart line to this:
ExecStart=/bin/sh -c '/usr/bin/setfont Lat2-Terminus16'

Related

Run shell script on reboot

Tried two methods and neither seem to be working:
crontab -e:
#reboot sleep 60;/home/linuxbox/script.sh
and created a service in /etc/systemd/system/script.service:
[Unit]
Description=a generic service to run on reboot
[Install]
WantedBy=multi-user.target
[Service]
ExecStart=/bin/bash /home/linuxbox/script.sh
Type=simple
User=linuxbox
Group=linuxbox
WorkingDirectory=/home/linuxbox
I follow that up with systemctl daemon-reload.
Not sure what is going wrong at this point -- any help is appreciated.
Have you enabled cron?
You can enable and start it with
sudo systemctl enable cron.service
The ExecStart command shouldn't be
/bin/bash /home/linuxbox/script.sh
but should be
/bin/bash -c "/home/linuxbox/script.sh".
Once you've created your script.service unit, you must of course enable it:
systemctl enable script.service
(This might seem obvious, but in qour question you only mention that you run systemctl daemon-reload which is not enough)

Systemd reload process gets killed

My systemd unit file looks something like this
start() {
java -jar server.jar &
}
reload() {
#do some application specific reload stuff
start
}
I'm realizing that right when the systemd call to reload finishes, the process running java -jar is actually dead. Systemd seems to think that my reload process is done and kills it. Is there any way to configure and cancel this process killing behavior? I have tried disowning the process, and messing with KillMode=blah and Type=blah in the service file, but no result.
Running on CentOS 7.4
I don't know if I understood your question, do you want the systemd unit file for executing server.jar, right?
Try it by following sequences.
First create your blah service unit file.
# vim /etc/systemd/system/blah.service
[Unit]
Description=blah service
After=network.target
Requires=network.target
[Service]
Type=simple
EnvironmentFile=/etc/sysconfig/blah
ExecStart=/usr/bin/java -jar server.jar
Restart=always
User=blah_USERNAME
[Install]
WantedBy=multi-user.target
And reload the blah service unit file.
# systemctl daemon-reload
Test it!
# systemctl start blah
# systemctl status blah
# systemctl restart blah
# systemctl status blah
I hope this will help you.

Register daemon controllable by start and stop command in Linux

Many system daemon can be started using start/stop command. I was just curious how start/stop works on Linux system. Say I wrote a daemon executable, how should I configure it so that it can be controlled by start/stop in Linux.
I make a daemon in linux (ArchLinux) few years ago, and it works every day perfectly.
There are 2 ways to do this. Short way and long way:
Short Way:
Create a file in /etc/systemd/system/ called for example mydaemon.service :
/etc/systemd/system/mydaemon.service
[Unit]
Description=This is my first daemon! - Fernando Pucci
After=network.target
[Service]
User=root
WorkingDirectory=/root
Type=oneshotmc
RemainAfterExit=yes
ExecStart=/bin/echo -e "Daemon started"
ExecStop=/bin/echo -e "Daemon Stopped"
[Install]
WantedBy=multi-user.target
This service does nothing but show Daemon Started or Stopped. You can change echoes by the sentences you need.
If you need to run some script, try the Long way:
Long way
Create a file in some directory, like root folder or /usr/lib/systemd/scripts called for example
/root/mydaemon.sh
start() {
<your start sentences here
and here>
}
stop() {
<your stop sentences here
and here>
}
case $1 in
start|stop) "$1" ;;
esac
You must to make it runnable (chmod x)
(And you can execute it with start or stop parameter to test it.)
And as second step, create another file in
/usr/lib/systemd/system/mydaemon.service
[Unit]
Description=Second daemon of Fernando Pucci
After=network.target
[Service]
User=root
WorkingDirectory=/root
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c '/root/mydaemon.sh start'
ExecStart=/bin/echo -e "MyDaemon Started"
ExecStop=/bin/bash -c '/root/mydaemon.sh stop'
ExecStop=/bin/echo -e "MyDaemon Stopped"
[Install]
WantedBy=multi-user.target
Starting and Stopping
systemctl start mydaemon
systemctl stop mydaemon
systemctl status mydaemon
systemctl enable mydaemon
systemctl disable mydaemon
You (and someone) can send me a private msg for help about that.

Fedora 20 how to run script at the end of startup

I am using Fedora 20. I have a two lines bash script needs to be run at the end of the startup. I want it to be run automatically each time when machine is startup. How can I do this?
I tried "sudo crontab -e" to insert my executable script but it always gave me error teling me the the time is not right and cannot modify the file.
You can create a Systemd unit file in /usr/lib/systemd/system/<service_name>.service. Here is a template:
[Unit]
Description=<description_string>
[Service]
WorkingDirectory=<working_directory>
Type=forking
ExecStart=/bin/bash <absolute_path_to_script>
KillMode=process
[Install]
WantedBy=multi-user.target
Replace anything in the angle brackets with your specific information. The 'WantedBy=multi-user.target' is the magic that tells Systemd to run your script on each start.
On the command line, tell Systemd to enable your service:
systemctl enable <service_name>.service
The next time you reboot your script should be run. Logs will be written to /var/log/messages.
Fedora has some basic documentation on unit files: Systemd unit files
You can append /etc/rc.local it runs just after the system starts up.
You may have to create it if doesn't exist:
Check this answer
Charlie's answer is better but you can still use Tiago's answer.
Just don't forget if you want to use /etc/rc.local way, grant execution permission to this file after editing:
chmod +x /etc/rc.local

How to run last and print my script output during boot with systemd?

I’m trying to configure my host during deployment process and to give an output to the screen of what my configuration script is doing.
In RHEL6 it was easy i was echoing what I want to screen or used dialog to display the output, and only when my script was done i got the login prompt.
( I used rc3.d or rc5.d folder with script name S99.myscript.sh)
In RHEL7 i can’t mimic this process.
rc.local does not display my output during booting and also its not guaranteed it will run last.
I guess I need to create a systemd service file that will run my script.
But how do I output the result to the screen while booting?
And how do I make sure I will not get the log-in prompt before my script ends?
below service example works like a charm :)
[Unit]
Description=ldt_bootscript1.service
After=network.target
Before=getty#tty1.service
[Service]
Type=oneshot
ExecStart=/bin/bash -c "/bin/bash /tmp/ldt_scripts/postinstall/rc.firstboot.qas | /usr/bin/dialog --clear --backtitle \"Linux Deployment\" --title \"tests\" --progressbox 20 70 > /dev/console 2>&1"
ExecStartPre=/usr/bin/echo -e \033%G
ExecReload=/bin/kill -HUP $MAINPID
RemainAfterExit=no
WorkingDirectory=/
Environment=TERM=xterm
[Install]
WantedBy=multi-user.target

Resources