The below init code works on ubuntu 14 however latest Ubuntu does not support init and need to convert this script to work in systemd.
I figured starting the service part however unable to figure out on how to restrict it to only tty2,3; how can I fix this?
(init-code)
# tty1 - getty
#
# This service maintains a getty on tty1 from the point the system is
# started until it is shut down again.
start on stopped rc RUNLEVEL=[23] and (
not-container or
container CONTAINER=lxc or
container CONTAINER=lxc-libvirt)
stop on runlevel [!23]
respawn
exec /sbin/getty -n -l /etc/init/banner.sh -8 <port> tty1
[Unit]
Description=This service maintains a getty on tty1 from the point the system is started until it is shut down again.
After=network.target
[Service]
Type=simple
ExecStart=/sbin/getty -n -l /etc/init/bapp_banner.sh -8 <Port> tty1
StandardOutput=syslog
StandardError=syslog
[Install]
WantedBy=multi-user.target
There are no runlevels in systemd, and tty1 are available and managed automagically until the system HAS to HANGUP on them.
Related
I'm trying to run BindToInterface in a script as part of a systemd service that runs under its own separate user.
The service file looks like this:
[Unit]
Description=Deluge Bittorrent Client Daemon
Documentation=man:deluged
After=network-online.target mnt-storage.mount
Requires=mnt-storage.mount
BindsTo=mnt-storage.mount
[Service]
Type=simple
UMask=000
ExecStart=/bin/bash /media/bti/deluged.sh
Restart=on-failure
# Time to wait before forcefully stopped.
TimeoutStopSec=300
[Install]
WantedBy=multi-user.target
The script is as follows:
#!/bin/bash
BIND_INTERFACE=eno2 DNS_OVERRIDE_IP=8.8.8.8 BIND_EXCLUDE=127.0.0.1,192.168. LD_PRELOAD=/media/bti/bindToInterface.so /usr/bin/deluged -d -l /var/log/deluge/daemon.log -L warning
when executing:
systemctl status deluged
I get the following output:
bash[503711]: setsockopt: Operation not permitted
Is there a way to run your tool without elevated privileges? How would "setsockopt" be run for a systemd unit without elevated privileges
Thanks
Linux as a strict limitation: Only root can open ports below 1024. If your daemon is doing this, you can only do it by root.
I have a script(sync.sh) which runs a while loop inside for syncing.
#!/bin/bash
while :
do
#my PHP scripts runs parallel
wait
sleep 60
done
I want to run this script independently forever in my vm.
I know I can run this sh file as a background process by using nohup, disown command.
But what I want to know is? How can I run this .sh file on system restart or it process is killed. How to start .sh file automatically without terminal command in Ubuntu VM.(Like we have starting Apache, MySQL services on system start)
Thanks in advance.
If you're using systemD, you should create a service for your script sync.sh, this file will be:
/lib/systemd/system/sync.service
You can edit this file (with 'root' or 'sudo' privileges) so it contains:
[Unit]
Description=My Shell Script for Sync
[Service]
ExecStart=/usr/bin/sync.sh
[Install]
WantedBy=multi-user.target
Then, you re-load your systemD daemon (so it knows that a service has been added) :
sudo systemctl daemon-reload
Then you can enable your service (so it will be launched at every system start:
sudo systemctl enable sync.service
Then you can start it manually so it will be started right away, not waiting for the next system restart :
sudo systemctl start sync.service
(of course, you can change the name of your service and it's not necessarily called "sync.service"
I wanted to run a bash script as root in startup. First I started using RC.Local and Crontab but nothing works.
Create the service file as in the template below and add the file in the location /etc/systemd/system/
And the Template as
[Unit]
Description = ~Name of the service~
[Service]
WorkingDirectory= ~directory of working file~
ExecStart= ~directory~/filename.sh
[Install]
WantedBy=multi-user.target
Start the service file by the name using
systemctl start servicefile.service
To enable on startup
systemctl enable servicefile.service
To check the status
systemctl status servicefile.service
To stop
systemctl stop servicefile.service
Create a systemd unit file in /etc/systemd/system/ and use it to execute your script. (i.e. hello-world.service).
[Unit]
Description=Hello world
After=sysinit.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=no
RemainAfterExit=yes
User=root
ExecStart=/bin/echo hello world
ExecStop=/bin/echo goodby world
[Install]
WantedBy=multi-user.target
Now you can use it through systemctl as you would with other services.
$ systemctl enable hello-world
$ systemctl start hello-world
$ systemctl stop hello-world
$ systemctl status hello-world
● hello-world.service - Hello world
Loaded: loaded (/etc/systemd/system/hello-world.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Wed 2019-10-09 13:54:58 CEST; 1min 47s ago
Process: 11864 ExecStop=/bin/echo goodby world (code=exited, status=0/SUCCESS)
Main PID: 11842 (code=exited, status=0/SUCCESS)
Oct 09 13:54:38 lnxclnt1705 systemd[1]: Started Hello world.
Oct 09 13:54:38 lnxclnt1705 echo[11842]: hello world
Oct 09 13:54:57 lnxclnt1705 systemd[1]: Stopping Hello world...
Oct 09 13:54:57 lnxclnt1705 echo[11864]: goodby world
Oct 09 13:54:58 lnxclnt1705 systemd[1]: Stopped Hello world.
Make sure that you use the full path to your script in the unit file (i.e. /bin/echo). Check out the documentation about keys used in hello-world.service:
[Unit]
[Service]
Place the script inside /etc/init.d
Ensure that it has the extension '.sh'
For the crontab,
there is difference in if you set up user crontab or root crontab:
$ crontab -e
#reboot sudo ...
^^ This is user's cron tab and wont work as it is.
$ sudo crontab -e
#reboot ...
^^ This is root's cron tab and will run comand as root.
The #reboot should do the trick for you of running scripts after startup.
I have installed certbot, and certbot makes it's own systemd service file "certbot.service" for auto cert renew, which is started trough a .timer file once a day.
After this "certbot.service" is sucessfully executed I like to execute a second one ("cert-copy-after-certbot.service") that copys the certificate to another place.
Currently my setting looks like this:
"certbot.service" (gernerated by certbot):
pi#raspberrypi:/lib/systemd/system $ cat certbot.service
[Unit]
Description=Certbot
Documentation=file:///usr/share/doc/python-certbot-doc/html/index.html
Documentation=https://letsencrypt.readthedocs.io/en/latest/
[Service]
Type=oneshot
ExecStart=/usr/bin/certbot -q renew
PrivateTmp=true
"cert-copy-after-certbot.service":
pi#raspberrypi:/etc/systemd/system $ cat cert-copy-after-certbot.service
[Unit]
Description=crt update after certbot has run
Wants=certbot.service
After=certbot.service
[Service]
Type=simple
ExecStart=/bin/sh -c "cat /etc/letsencrypt/live/<mydomain>/privkey.pem /etc/letsencrypt/live/<mydomain>/fullchain.pem > /etc/ejabberd/ejabberd.pem"
If i run this files with:
systemctl start <unitname>
Both services are working.
But when I start certbot with "systemctl start certbot" and check
systemctl status cert-copy-after-certbot
the cert-copy-after-certbot.service didn't run.
Did i configured something wrong?
I found the solution, so here the answer just if someone has the same issue.
The problem is that the "certbot.service" unit don't know about "cert-copy-after-certbot.service". So if "certbot.service" is called no one calls the inactive "cert-copy-after-certbot.service" because the "Wants=" is never executed.
So if you don't wan't to alter the "certbot.service" unit (with "Wants=cert-copy-after-certbot.service", you can do the following.
Add an additional [Install] section in "cert-copy-after-certbot.service", with a line "WantedBy=cerbot.service". So that the file look like this:
pi#raspberrypi:/etc/systemd/system $ cat cert-copy-after-certbot.service
[Unit]
Description=crt update after certbot has run
After=certbot.service
[Service]
Type=simple
ExecStart=/bin/sh -c "cat /etc/letsencrypt/live/<mydomain>/privkey.pem/etc/letsencrypt/live/<mydomain>/fullchain.pem > /etc/ejabberd/ejabberd.pem"
[Install]
WantedBy=certbot.service
An install section requires an enable or disable call by systemctl (or start or stop for temporary testing).
systemctl enable cert-copy-after-certbot
This [Install] section will create a symbolic link as soon as you enable the unit that informs the systemd deamon if "certbot.service" is called, he have to call "cert-copy-after-certbot.service" to. (And the "After=" in the unit section tells systemd the row in which the sould called, without it, both units would run simultaneously)
You might find that cert-copy-after-certbot.service is started before cerbot.service is complete unless you also set RemainAfterExit=yes in cerbot.service
I have a script file as below.
#!/bin/bash
set -x
set -e
#VBoxManage startvm "cuckoo-window" --type gui
python ~/Downloads/cuckoo-modified-master/utils/api.py --host 0.0.0.0 --port 8090
#cd ~/Downloads/cuckoo-modified-master/web/
# python manage.py runserver 0.0.0.0:8008
# python ~/Downloads/cuckoo-modified-master/cuckoo.py
My service script /etc/init/miscservices.conf
start on runlevel
script
cd ~/Downloads/cuckoo-modified-master
./miscservices.sh
end script
I have also created a symlink in /etc/init.d/miscservices and added in startup
sudo update-rc.d miscservices defaults
sudo service miscservices start
miscservices stop/waiting
No script started. When I start as below, them move to root mode but still no service start. But when I exit it start 2 instances of the service.Please explain this behavior.
sudo /etc/init.d/miscservices start
start: Unknown job: on
Script started, file is typescript
root#abc:~# sudo netstat -ntlp | grep 8090
root#abc:~# ps -aux | grep misc
root 2929 0.0 0.0 81976 2260 pts/6 S+ 13:42 0:00 sudo /etc/init.d/miscservices start
root 2930 0.0 0.0 4440 652 pts/6 S+ 13:42 0:00 sh /etc/init.d/miscservices start
root 2962 0.0 0.0 16192 936 pts/15 S+ 13:43 0:00 grep --color=auto misc
root#abc:~#
root#abc:~#
root#abc:~# exit
exit
Script done, file is typescript
+ set -e
+ python /home/aserg/Downloads/cuckoo-modified-master/utils/api.py --host 0.0.0.0 --port 8090
Bottle v0.12.0 server starting up (using WSGIRefServer())...
Listening on http://0.0.0.0:8090/
Hit Ctrl-C to quit.
I think the problem may be in ~/Downloads/cuckoo-modified-master/miscservices.sh script (and btw use ~ in upstart script not best idea because it will probably execute as root and there can be some problems with determitate where is ~). By default upstart doesn't spawn processes. That mean if you have your configuration like that:
start on runlevel
script
cd ~/Downloads/cuckoo-modified-master
./miscservices.sh
end script
upstart will only start your process once and then do nothing. And if you start it once and there was some error in script it just stoped with no information to you. You can check log file. By default it must be in /etc/log/upstart/miscservices.log (for Ubuntu 14.04 LTS). If no you can check where is default upstart logs in your OS or write it manually just by echo some informtaion in certain place. For example:
env logf="/home/someuser/miscservices.log"
script
echo "Script it starting..." >> $logf
/home/someuser/Downloads/cuckoo-modified-master/miscservices.conf >> $logf
echo "Script is ended." >> $logf
end scipt
And if you want to respawn script processes you may add to the beggining of the script:
respawn
respawn limit unlimited
or just make unlimited loop in script file.
ps. You can modify your upstart script /etc/init/miscservices.conf by more clear code. From start on runlevel to:
start on filesystem
stop on shutdown
This mean that script will start after filesystem starts and will shutdown on shutdown.
pps. You dont need symlink in /etc/init.d/miscservices. If you use upstart then just use it! You dont need anything else. Just put your end scipt in /etc/init and it will start automatically. It can be something like that:
start on filesystem
stop on shutdown
env logf="/home/someuser/miscservices.log"
script
echo "Script it starting..." >> $logf
/home/someuser/Downloads/cuckoo-modified-master/miscservices.conf >> $logf
echo "Script is ended." >> $logf
end scipt
Hope i can help!