You are not logged in.

#1 2013-01-17 01:02:15

xworld
Member
Registered: 2012-05-27
Posts: 153

[SOLVED]Custom Scripts on Startup in Systemd

Hello archers. I'm a bit embarassed about a problem that I've been trying to solve and can't. I want to know how to have custom bash scripts run at startup in systemd. Currently I'm trying to get this one to run at startup:

#! /bin/sh
sudo ifconfig wlan0 down
sudo macchanger -r wlan0
sudo ifconfig wlan0 up
sleep 1
echo -e ' \t '
echo "Mac address successfully changed."
exec bash

It doesn't seem to work. I looked up mac address spoofing in the Arch wiki and tried my best to follow this:

 Systemd Unit

Same thing with systemd:

/etc/systemd/system/macspoof@.service

[Unit]
Description=MAC address change %I
Before=dhcpcd@%i.service

[Service]
Type=oneshot
ExecStart=/usr/sbin/ip link set dev %i address 36:aa:88:c8:75:3a
ExecStart=/usr/sbin/ip link set dev %i up

[Install]
WantedBy=network.target

No dice. Here's what mine looks like:

[Unit]
Description=MAC address change %I
Defore=dhcpcd@i.service

[Service]
Type=oneshot
ExecStart=sudo ifconfig wlan0 down
ExecStart=sudo macchanger -r wlan0

[Install]
WantedBy=network.target

There's obviously something I'm missing and Google doesn't seem to be yielding ANY results related to running custom scripts at startup in systemd.

Thanks for reading.

Last edited by xworld (2013-02-12 05:59:29)

Offline

#2 2013-01-17 01:23:31

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED]Custom Scripts on Startup in Systemd

You have a pretty glaring typo in the service file... And I don't think you will need sudo.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2013-01-17 01:30:59

chris_l
Member
Registered: 2010-12-01
Posts: 390

Re: [SOLVED]Custom Scripts on Startup in Systemd

You didn't needed to create a service based on macspoof@.service, you only needed to enable it adding your wlan device after the @. Like this:

systemctl enable macspoof@wlan0.service

(assuming you still use wlan0 as your wifi device)
Read man systemd.unit to understand what %i means.
And, unless is a service executed by systemd user session service or you are using the User= parameter, the services are executed by root, so sudo is not necessary.

Also, since systemd does not execute the commands by using a shell, but using an exec() call, is better you don't expect it to guess the path.
So, change lines like this:

ExecStart=sudo macchanger (...)

to this:

ExecStart=/usr/bin/macchanger (...)

(I'm guessing macchanger is on /usr/bin; I don't know, I don't have it installed. Find your path using whereis macchanger)

And one last thing, ifconfig is deprecated, use ip instead.


"open source is about choice"
No.
Open source is about opening the source code complying with this conditions, period. The ability to choose among several packages is just a nice side effect.

Offline

#4 2013-01-17 01:49:35

xworld
Member
Registered: 2012-05-27
Posts: 153

Re: [SOLVED]Custom Scripts on Startup in Systemd

Ah yes, I guess 'before' is not spelled with a D. And I see I missed a % in there as well. Thank you guys for your fast responses. So instead of ifconfig wlan0 down would I do something like:

ip link set wlan0 down?

Also, something I've always noticed is that I can run ifconfig wlan0 down and then change the mac but running ifconfig wlan0 up never works. I have to manually click on networkmanager and then reconfigure my network in order to be able to connect to the internet.

Hmm. I must be doing something wrong still. Here's my new file:

[Unit]
Description=MAC address change %I
Before=dhcpcd@%i.service

[Service]
Type=oneshot
ExecStart=/sbin/ifconfig wlan0 down
ExecStart=/usr/bin/macchanger -r wlan0

[Install]
WantedBy=network.target

It still doesn't want to work.

Last edited by xworld (2013-01-17 02:02:17)

Offline

#5 2013-01-17 02:02:18

chris_l
Member
Registered: 2010-12-01
Posts: 390

Re: [SOLVED]Custom Scripts on Startup in Systemd

Well ifconfig wlan0 up would only set the card up; it wont do the wpa/wep things, or get you an address with dhcpcd, etc.
That is, it will set the card as "up", but it wont reconnect it to the lan.

Thats the reason the macspoof service has Before=dhcpcd@%i.service; to make the mac change before it tries to get an ip with dhcpcd.

EDIT: I think I'm missing something. Is there a reason for not just doing

systemctl enable macspoof@wlan0.service

instead of creating a custom systemd service?

Last edited by chris_l (2013-01-17 02:09:05)


"open source is about choice"
No.
Open source is about opening the source code complying with this conditions, period. The ability to choose among several packages is just a nice side effect.

Offline

#6 2013-01-17 02:07:32

xworld
Member
Registered: 2012-05-27
Posts: 153

Re: [SOLVED]Custom Scripts on Startup in Systemd

Ah, I understand. So if I wanted a custom script to run at startup would i put the path to that script in the ExecStart? Because so far nothing seems to work.

Offline

#7 2013-01-17 02:49:40

chris_l
Member
Registered: 2010-12-01
Posts: 390

Re: [SOLVED]Custom Scripts on Startup in Systemd

To execute an script, call it as argument of the interpreter. In another words:

ExecStart=/bin/bash /etc/myscript.sh

"open source is about choice"
No.
Open source is about opening the source code complying with this conditions, period. The ability to choose among several packages is just a nice side effect.

Offline

#8 2013-01-17 03:26:19

xworld
Member
Registered: 2012-05-27
Posts: 153

Re: [SOLVED]Custom Scripts on Startup in Systemd

Ok thanks very much. I suppose I should still use the other variables in the macspoof script that I've been working with. I can't figure out why it won't run at start-up. I also suppose I'm on my own from here.

Thanks again for the help.

EDIT: How foolish of me. It works now. I forgot to do:

systemctl start macspoof@wlan0.service

The only trouble is that it doesn't open up a terminal and then run the commands I asked it to, even though the functionality is all there. Here's the script it's running:

#! /bin/sh
sudo ifconfig wlan0 down
sudo macchanger -r wlan0
sudo ifconfig wlan0 up
sleep 1
echo -e ' \t '
echo "Mac address successfully changed."
exec bash

But instead of opening a terminal and then printing out what I asked it to after changing the mac it simply changes the mac. Which is good. But, is there something I'm missing?

Last edited by xworld (2013-01-17 05:04:33)

Offline

#9 2013-01-17 16:25:53

chris_l
Member
Registered: 2010-12-01
Posts: 390

Re: [SOLVED]Custom Scripts on Startup in Systemd

xworld wrote:

But instead of opening a terminal and then printing out what I asked it to after changing the mac it simply changes the mac. Which is good. But, is there something I'm missing?

You are missing the fact that "not opening a terminal" is the expected behavior tongue

Maybe you'll see a log of what happened by doing systemctl status macspoof@wlan0.service

But, there is a point on doing that?


"open source is about choice"
No.
Open source is about opening the source code complying with this conditions, period. The ability to choose among several packages is just a nice side effect.

Offline

#10 2013-01-17 16:30:10

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: [SOLVED]Custom Scripts on Startup in Systemd

Why would it open a terminal, you didn't tell it to?  What terminal do you want it to use?  And what is the "exec bash" at the end for?  That will do absolutely nothing useul.  Nor will the sudo (which was pointed out above).

Last edited by Trilby (2013-01-17 16:31:00)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#11 2013-01-17 23:42:44

xworld
Member
Registered: 2012-05-27
Posts: 153

Re: [SOLVED]Custom Scripts on Startup in Systemd

I realized today while I was thinking about it that I didn't actually give a command to open the terminal. And the exec bash is because before I did that it printed everything on the screen and exited so fast that I couldn't read any of it. So exec bash was to keep the terminal open after it was done.

It was kind of an experimental script.

Offline

#12 2013-01-18 00:52:41

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: [SOLVED]Custom Scripts on Startup in Systemd

Most terminals can be passed a parameter to stay open after the command passed to them completes.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#13 2013-01-18 03:02:04

xworld
Member
Registered: 2012-05-27
Posts: 153

Re: [SOLVED]Custom Scripts on Startup in Systemd

Exec bash was the one I landed on. Although I imagine there are better ways.

Offline

#14 2013-01-18 05:22:17

Stebalien
Member
Registered: 2010-04-27
Posts: 1,237
Website

Re: [SOLVED]Custom Scripts on Startup in Systemd

I known that this is somewhat tangential, but why aren't you using a udev rule (it doesn't require bringing the interface down)?

SUBSYSTEM=="net", ENV{DEVTYPE}=="wlan", RUN+="/usr/bin/macchanger -r $env{INTERFACE}"

Steven [ web : git ]
GPG:  327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C
Do not email: honeypot@stebalien.com

Offline

Board footer

Powered by FluxBB