You are not logged in.

#1 2019-11-29 05:00:47

BLizgreat!
Resident Babbler - vll!
Registered: 2015-10-03
Posts: 1,217

Easy way to run a script when your system starts. ;)

Really simple way of running a script at OS bootup. Things have changed and while there's still tons of ways to do this on gnu/Linux here's the way I prefer, I want to echo various settings to system files to tweak my OS. One widely used older way was to do this with the /etc/rc.local file, however with systemd around this is now discouraged and a better way would be to create a systemd unit file(service) etc. Backwards compatiblity is still around, if there's a valid rc.local file in /etc and it's executable "sudo chmod +x /etc/rc.local" then systemd will run the thing via the rc-local.service. I like systemd and decided to get rid of the old way, as it's no longer encouraged and hacky now. Whew ... with all that out of way, let's proceed.

We're going to use an @reboot crontab to run the script. That script will be run at every boot/reboot. First order of business is to create a script. Of course many ways fellow nixers, we're going to do the gooey(gui)style. So open your file-manager of choice with elevated privileges and create a file(script) at the following location,

/usr/local/bin/yourscript.sh

Obviously yourscript.sh is the name of the script you're making. Do make sure it has the .sh extension fellows and begins with "#!/bin/bash". Now select "open a terminal here" and make it executable "sudo chmod +x yourscript.sh", open the thing up with your prefered text-editor and add whatever you like. Here's the contents of the script I'm running by way of example.

#!/bin/bash
# Echoing setting adjustments to various virtual files.
echo 256 > /sys/block/sda/queue/read_ahead_kb &
echo 256 > /sys/block/sda/queue/nr_requests &
echo 2 > /sys/block/sda/queue/rq_affinity &
echo 0 > /sys/block/sda/queue/add_random &
# Adjusting ondemand governor settings. As per/was in rc.local
echo 70 > /sys/devices/system/cpu/cpufreq/ondemand/up_threshold &
echo 5 > /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor &
echo  25000 > /sys/devices/system/cpu/cpufreq/ondemand/sampling_rate &

NOTE: DO NOT just apply these yourself, unless you know what they do/mean etc. Had to add this as the standard disclaimer. Anything and/or everything you see related to just about anything comes without warranty ... express or implied folks. So yeppers, best to learn enough about what you're doing to know what's good/bad eh. smile

Alright, at this point I have my script at the location I want /usr/local/bin/myscript.sh, the thing is executable and now to make sure it gets ran when my OS boots-up. Make your user root with any of these commands in terminal.

su -
sudo su -
sudo -i

Then setup an @reboot crontab for root ...

crontab -e

Scroll down to the obvious place and add this ...

@reboot /usr/local/bin/yourscript.sh

Save the file, close it and there you have it, that sucker gets run every boot. You've just setup a root crontab, to automagically run a script when the OS boots. To check, in my case I can do so with ie:

cat /sys/block/sda/queue/read_ahead_kb

Yep, it returns the value I wanted = 256 for it, YAY! smile

As an added bonus to this I can now remove rc.local so one less service rc-local.service is automatically started at every bootup. Just to be uber anal I even disabled the thing with the "systemctl" command ... Potentially saving some tiny amount of boot-time and system resources. While still getting the system settings I wanted set via a startup script.

sudo systemctl disable rc-local.service

NOTE: Also your normal user can setup crontabs too. Though for what I'm doing above, it's just easier to run them as root-user due to file permissions and so forth. Anyway ... ENJOY !!! big_smile

Last edited by BLizgreat! (2019-11-29 05:10:10)

Offline

#2 2019-11-29 08:07:00

ohnonot
...again
Registered: 2015-09-29
Posts: 4,168
Website

Re: Easy way to run a script when your system starts. ;)

  1. I'm pretty sure those settings can be tweaked by editing some sysctl file under /etc - no cron job required

  2. I'm not sure it's a good idea to disable the rc-local.service

  3. IMO it would be overall better to implement things like this directly in systemd

Offline

#3 2019-11-29 08:43:38

BLizgreat!
Resident Babbler - vll!
Registered: 2015-10-03
Posts: 1,217

Re: Easy way to run a script when your system starts. ;)

^ Well I appreciate your feedback but you don't know what you're talking about. smile

Offline

#4 2019-11-29 08:49:51

twoion
ほやほや
Registered: 2015-08-10
Posts: 2,553

Re: Easy way to run a script when your system starts. ;)

The systemd-native mechanism for this kind of thing is tmpfiles (man tmpfiles.d).

 $ cat /etc/tmpfiles.d/configure_trackpoint.conf 
w /sys/devices/platform/i8042/serio1/serio2/inertia - - - - 4
w /sys/devices/platform/i8042/serio1/serio2/speed - - - - 255
w /sys/devices/platform/i8042/serio1/serio2/sensitivity - - - - 196

At the end of the river the sundown beams

Offline

#5 2019-11-29 09:44:55

BLizgreat!
Resident Babbler - vll!
Registered: 2015-10-03
Posts: 1,217

Re: Easy way to run a script when your system starts. ;)

^ Thanks for the add Twoion, you do tend to actually bother to know that of which you speak and enjoy reading some of your posts. Mentioned other ways, involving systemd etc. Though none of this is the point or topic of this thread. Was using something I do and works fine as an example, with warnings and disclaimer attached. Is certainly not a you/others do this kind of an example. Isn't at all ... As title and post make pretty dang clear the thread is about running a script at OS startup. Such a script could be/do a gazillion things or with a tiny tad of googling could be set to run whenever a person wishes and sets up via crontab etc. Blah and blah + blahblah. smile

Last edited by BLizgreat! (2019-11-29 09:47:45)

Offline

#6 2019-11-29 14:24:05

clusterF
Member
Registered: 2019-05-07
Posts: 322

Re: Easy way to run a script when your system starts. ;)

BLizgreat! wrote:

^ Well I appreciate your feedback but you don't know what you're talking about. smile

https://cromwell-intl.com/open-source/p … disks.html

scroll down to...

Automatically Tuning the Schedulers

Sysfs is an in-memory file system, everything goes back to the defaults at the next boot. You could add settings to /etc/rc.d/rc.local:

... preceding lines omitted ...

## Added for disk tuning this read-heavy interactive system
for DISK in sda sdb sdc sdd
do
	# Select deadline scheduler first
	echo deadline > /sys/block/${DISK}/queue/scheduler
	# Now set deadline scheduler parameters
	echo 100 > /sys/block/${DISK}/queue/iosched/read_expire
	echo 4 > /sys/block/${DISK}/queue/iosched/writes_starved
done 

If the old way still works, why is it discouraged because of systemd? I cant see anything hacky about it at all.

Last edited by clusterF (2019-11-29 14:37:55)


"Common sense is like deodorant, those who need it the most never use it."

git: clusterF

Offline

#7 2019-11-29 21:27:25

BLizgreat!
Resident Babbler - vll!
Registered: 2015-10-03
Posts: 1,217

Re: Easy way to run a script when your system starts. ;)

When comes to gnu/nix many times, many ways to go about xyz, all can be approved and/or work. Very possibly just as well as the other. For why I'm abandoning rc.local, that's just in the interests of keeping things current and clean. This is a case where uphill(stream) developments are prompting me to move on with how something is done. Fact that it's(rc.local) is hacked around and considered a backwards compatibility thing indicates there is a more recent and better approved method of going about something. Though yep, use of rc.local is still supported. Not sure as to how long that'll remain ... Could be indefinitely but why not do it the approved way now and skip relying on something outmoded. It's really something to take up with a persons favorite search engine(s.)

Will look over the link you took time to provide, thanks clusterF. There's always something(more) to learn with gnu/Linux. If that info provides a better/cleaner way of accomplishing something I'm doing, yep ... will use it. What I'm doing above and as far as I'm aware is considered perfectly good practice regardless. Certainly isn't going to cause any problems. One refinement though, rather than using bash here ... /bin/bash better to use /bin/sh to use the system shell = dash. Noted in the OP the absolute most approved of method would likely be considered using a systemd unit file, runs the script at the exact best point to apply whichever settings adjustments, then exits. Though what I'm going with is perfectly fine too. Overall the main thing here was to show one way of automatically running a startup script. Someone is of course free to go about anything they wish, however they wish, in an approved of way or not but this babble tute is about using an @reboot crontab to do this thing.

Last edited by BLizgreat! (2019-11-29 21:31:47)

Offline

#8 2019-11-30 07:12:06

ohnonot
...again
Registered: 2015-09-29
Posts: 4,168
Website

Re: Easy way to run a script when your system starts. ;)

BLizgreat! wrote:

^ Well I appreciate your feedback but you don't know what you're talking about. smile

That must be hands down the shortest post you made, ever!
One could almost get the impression that you think that I'm trying to insult you.
Because I'm not.
I just wanted to register my disagreement, and make sure this post stands as what it is: one user's opinion, no more, no less.

I had a closer look at what you're trying to achieve there - apparently these settings cannot be tweaked in /etc/sysctl*, so I was wrong about that. Btw it would be nice if you also explained what you're actually tweaking there and why it's a good idea to do so.

What threw me off is that you recommend people to disable rc-local.service - because this service is not enabled by default, and en/disabling it will have no effect because it is static. My guess is that it's part of the overall sysVinit compatibility layer and will get executed on bootup if /etc/rc.local exists.
Disabling it will do no harm though, so arguably I was wrong about that, too.

But since you yourself say that you want to do things the systemd way, I don't see why you use it to access a legacy service (there's no cron on e.g. ArchLinux because all of its functionality can be achieved with systemd) for something that can be achieved with systemd only - various ways (thanks to twoion for pointing out tmpfiles.d).

I'm not saying one isn't allowed to do all that though; my post stands here as my opinion, no more, no less.

Peace.

Last edited by ohnonot (2019-11-30 07:13:46)

Offline

#9 2019-11-30 15:03:12

clusterF
Member
Registered: 2019-05-07
Posts: 322

Re: Easy way to run a script when your system starts. ;)

legacy... outdated but still in use?

Legacy inits = old school hammer and nails
Systemd = nailgun

Last edited by clusterF (2019-11-30 15:20:52)


"Common sense is like deodorant, those who need it the most never use it."

git: clusterF

Offline

#10 2019-11-30 19:32:19

BLizgreat!
Resident Babbler - vll!
Registered: 2015-10-03
Posts: 1,217

Re: Easy way to run a script when your system starts. ;)

^ Pretty much. smile

@Ohnonot ... yeah I did take it that way due to recent tensions. Got around to doing something, thought hey, why not do a write up about it while on my mind so others can use the info and decided why not BL forum, blahblahblah. Then your post did come off as insulting. One reason being because yeah you clearly didn't/don't understand how much of what's being discussed works, I do, I've taken the time + effort etc to learn what I'm talking about. So baseless opinions and what xyz-person "thinks", the "IMO" vs someone who actually takes pains to know, possibly has something meaningful to contribute to a thread is irritating.

Yeppers, sysctl.conf has zero to do with this, mentioned in OP, if there isn't an rc.local file that's executable in /etc then rc-local.service doesn't run anyway. Again anyone who wants to can investigate why rc.local isn't so much a good practice, esp now with systemd having replaced SysV(google)and did point out that a systemd unit file is also an option. What I'm doing in a script being used as an example is totally irrelevant to the content of this babble tute. Anyone interested can do as clusterF did, highlight one of lines in that script, right click ... search google.

As for you saying cron not available in Arch, errrr, ok, this isn't a how-to in the Arch forum and that distro with it's "Arch way" expects people to learn, gain actual knowledge, do for themselves, thusly if they want to set up a crontab, they'll read the docs and wiki and do so. However cron, crontabs are commonly available on a huge range of distro's and no doubt are in BL, are on my OS's too. I'm not going to touch any/every related single possible thing associated with putting up a useful and interesting how-to for free on a gnu/Nix forum. All info related to every aspect of it is already online, GOOGLE !!! Painfully clearly this is not a hey, what do you think about my script, not a don't use rc.local do this instead etc etc etc, it's one way to run a friggin script at startup. Nothing more, nothing less.

Opinions (and thoughts) are like aholes, everybodys got em. However if you don't bother actually knowing wth you're talking about, are basing your thinks/JMO's etc without having a clue then please peeps, KEEP THEM TO YOURSELF !!! Nothing cool, interesting or useful to offer on the topic, please remain quiet and keep your arms, legs etc in the vehicle at all times ...

NO PUPPIES/KITTENS WERE HARMED DURING THE TYPING OF THIS POST. Awwwwwww, who doesn't like pups/kits !?!?!? tongue

Last edited by BLizgreat! (2019-11-30 19:38:29)

Offline

#11 2019-12-01 06:29:37

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,097
Website

Re: Easy way to run a script when your system starts. ;)

@BLZ we all appreciate the time and effort you put into researching all this stuff, and then writing about it for others on busier schedules to benefit from, but you're surely not saying that no-one can comment in your threads unless they've already got all the qualifications and credentials lined up? We're a fairly easy-going place here, and even "I think" or "maybe" or "I wonder" or "I might remember having read somewhere" are allowed. smile


John
--------------------
( a boring Japan blog, idle Twitterings and GitStuff )
In case you forget, the rules.

Offline

#12 2019-12-01 07:46:37

BLizgreat!
Resident Babbler - vll!
Registered: 2015-10-03
Posts: 1,217

Re: Easy way to run a script when your system starts. ;)

If you're a clueless tard or azzhat, no don't post baseless nonsense in a babble tute I took my time to write. Basic common decency says don't take a cyber-crap in someone's how-to, ESP when x-person clearly doesn't have the slightest clue what they're talking about anyway. Man again ... just gotta call it done. Next time I think about posting here, will just step up to the nearest wall in my apartment and bang my head on the thing until I get a headache. Will be more rewarding, same headache, only more efficient as there will be less steps, less aggravation and typing involved. big_smile Ah anyway, my mistake won't happen again.

Offline

#13 2019-12-01 14:47:33

clusterF
Member
Registered: 2019-05-07
Posts: 322

Re: Easy way to run a script when your system starts. ;)

^ cheer up bloke, its only words on a screen.


"Common sense is like deodorant, those who need it the most never use it."

git: clusterF

Offline

Board footer

Powered by FluxBB