LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-16-2018, 12:11 PM   #1
aristosv
Member
 
Registered: Dec 2014
Posts: 153

Rep: Reputation: Disabled
systemd run a script on boot


On Debian 9, I want to run a bash script on boot. So I did this:

/etc/systemd/system/iptable
Code:
#!/bin/bash
ip addr add 192.168.1.40/24 dev $(ip addr show | awk '/inet.*brd/{print $NF; exit}')
iptables -t nat -A PREROUTING -d 192.168.1.40 -p tcp --dport 22 -j REDIRECT --to-port 5040
/etc/systemd/system/iptable.service
Code:
[Unit]
Description=configure iptable
After=network.target
[Service]
Type=simple
ExecStart=/etc/systemd/system/iptable
TimeoutStartSec=0
[Install]
WantedBy=default.target
systemctl start iptable ; systemctl status iptable
Code:
● iptable.service - configure iptable
   Loaded: loaded (/etc/systemd/system/iptable.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Wed 2018-05-16 20:00:42 EEST; 15s ago
  Process: 427 ExecStart=/etc/systemd/system/iptable (code=exited, status=0/SUCCESS)
 Main PID: 427 (code=exited, status=0/SUCCESS)
May 16 20:00:42 linuxhost systemd[1]: Started configure iptable.
I know the script runs, because the ip is up, but is the status of the service correct? It says inactive (dead). Is there a more appropriate way to run this script on boot?
 
Old 05-16-2018, 05:30 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 17,483

Rep: Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284
Try adding the following. The idea of your script is to run a single task and exit so I would expect it to be inactive once it has been run. The RemainAfterExit will just force systemd to show it as active instead of inactive after the process has exited.

Type=oneshot
RemainAfterExit=yes


Not enough information as to how your system is configured but I would probably go setup the alias in Network Manager or in the interfaces file if it isn't running and permanently add the rule using whatever firewall or CLI tool is installed in debian 9.
 
1 members found this post helpful.
Old 05-16-2018, 07:47 PM   #3
JeremyBoden
Senior Member
 
Registered: Nov 2011
Distribution: Debian
Posts: 1,344

Rep: Reputation: 272Reputation: 272Reputation: 272
Or just use cron.
 
Old 05-16-2018, 08:17 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 17,483

Rep: Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284Reputation: 2284
True and you can also run the commands from rc.local.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Fedora26 (Rawhide)-- have successfully configured system to boot and run without any running instances of systemd firejuggler86 Fedora 7 11-22-2016 02:29 PM
LXer: How to automatically execute shell script at startup boot on systemd Linux LXer Syndicated Linux News 0 07-07-2016 10:21 AM
Can't access full journalctl from script via systemd service even though user is in systemd-journal group iwtbf Linux - Newbie 0 02-19-2016 02:44 PM
[SOLVED] run script on boot with systemd Mario Blunk SUSE / openSUSE 2 09-23-2014 11:12 AM
Boot Delay 30min: systemd-analyze blame systemd-tmpfiles-setup.service BGHolmes Fedora 0 07-27-2011 09:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration