Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Running Bash scripts at startup
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
jose.maria
n00b
n00b


Joined: 04 Sep 2012
Posts: 24
Location: In the garden

PostPosted: Fri Oct 05, 2012 7:27 pm    Post subject: Running Bash scripts at startup Reply with quote

As part of a tool I am programming, there are two bash scripts that should be run as root and as the user. The first one, say script.root, is currently located at /etc/local.d in order to run it at start up as root. The second one, say script.user, is located at $HOME/.kde4/Autostart in order to run it when the user logs in.

I am at the beginning of the project. Until now I found a solution that works nicely in my system and I would like to make a program to improve the usability of the solution. So my problem is how to make things work in general.

script.root can be copied at installation time into the aforementioned directory, assuming the command 'make install' is run by root. There is no problem with that. But the second, script.user, should be launched when the user logs in. It depends on whether he/she uses kde, gnome or other desktop environment.

I have the following questions:

A) Is there a way to run script.user as the user automatically at startup independently of the Linux distribution and desktop environment?
B) Are the startup scripts always located at /etc/local.d independently of the Linux distribution one uses? Or each distribution has its ways of running scripts at startup?

Any ideas?
Back to top
View user's profile Send private message
eccerr0r
Watchman
Watchman


Joined: 01 Jul 2004
Posts: 9645
Location: almost Mile High in the USA

PostPosted: Sat Oct 06, 2012 7:07 pm    Post subject: Reply with quote

One thing that could be a problem is that users changed their default shells. If you're sure all users did not change their shells, most of the time users go through /etc/profile (for bourne shell users) on login. For c-shells and their derivatives, it seems that /etc/profile.csh is their analog. However there are many was to get around this too...

As for the other question - startup scripts - no, each Linux flavor tends to be different. Even worse, some use systemd instead of the usual sysvinit or even openrc. You'll have to manually detect what system they're using and try to put it in the right spot...
_________________
Intel Core i7 2700K/Radeon R7 250/24GB DDR3/256GB SSD
What am I supposed watching?
Back to top
View user's profile Send private message
steveL
Watchman
Watchman


Joined: 13 Sep 2006
Posts: 5153
Location: The Peanut Gallery

PostPosted: Sun Oct 07, 2012 6:57 am    Post subject: Reply with quote

eccerr0r wrote:
You'll have to manually detect what system they're using and try to put it in the right spot...

Yuck, don't do that. It's better to leave it up to distributions, or end-users, as part of the normal packaging process.

The real problem is reliance on BASH, if you want people to use it every where.

Just write a POSIX-sh compatible initscript (it's ok to use local vars) for openrc, and if you have a debian install, do a SysV initscript as well (same syntax, different setup.) If not, try it in a virtual machine. Add a systemd unit-file, and you have all the distros covered. ISTR Solaris is System-V, and you might want to check out what say, FreeBSD does; they have good docs (bear in mind openrc is BSD-compatible.)

I'd start just by getting something POSIX-compatible working under openrc, as a service, not an addendum to local. #openrc can advise, #bash are actually very good on sh, so long as you state clearly that you're working in sh and not bash up-front, and you can always ask in #friendly-coders (all on IRC chat.freenode.net) if you're stuck (I'm `igli'.)

As for something that needs to be run per-user, while it might feel convenient just to append to /etc/profile, it's not your package's decision to make: it has to be left to the sys-admin or user to do; you can provide a small shell-script that prints the settings needed to stdout, and ewarn in the ebuild.

Once you have the above working, you can look at writing .deb and .rpm files for other distributions, as part of your package, which they can patch and feedback to you.

HTH,
SteveL.
Back to top
View user's profile Send private message
jose.maria
n00b
n00b


Joined: 04 Sep 2012
Posts: 24
Location: In the garden

PostPosted: Sun Oct 07, 2012 2:30 pm    Post subject: Reply with quote

Wow! Thank you for the advices. I still have to digest them. It is the first time I do something that could be used by others (if you obviate my PhD thesis software, which only a bunch of people would find useful). I definitely will visit the IRC channels you mentioned. Thanks again!
Back to top
View user's profile Send private message
steveL
Watchman
Watchman


Joined: 13 Sep 2006
Posts: 5153
Location: The Peanut Gallery

PostPosted: Fri Oct 12, 2012 12:18 pm    Post subject: Reply with quote

jose.maria: You popped into #friendly-coders the other day, but I was just crashing out, so didn't see it til next time I came online, in the back-buffer. Sorry you didn't get much response about bash (the channel is fairly quiet usually, till someone needs explanation of what another channel was talking about, but too rudely to deal with ;)

I take it you've visited #gentoo and #bash? Don't forget #awk and #sed (remember awk is perhaps the most portable utility available, since it is often installed on windows even when nothing else is.)

And if you want to learn where all the text editing utilities come from, learn #ed. sed, awk, ex, vi, and vim all descend from ed, so when you know its commands, you gain deeper insight into all of those. And g/re/p speaks for itself :) Links at end.

Hope to see you again, and we can have a proper chat about bash-scripting. Keep #bash in your irc client autojoin list: it really is an education. I'd say it took me 2 years til I got to the stage where I felt confident, and #bash is where I learnt everything I know about portable shell-scripting: if greycat says anything, pay close attention. twkm, TheBonsai and lhunath as well. Though twkm says little, he is a code genius: he talks more in ##posix ##c and #workingset (the latter is for build-tools like make, autotools, cmake and so on.)

Regards,
`igli'

Bash:
greycat's site is the best resource on the net:
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashPitfalls

TheBonsai's site gives a gentle introduction for people new to scripting:
http://wiki.bash-hackers.org/doku.php?id=scripting:basics

These are good for portability and the "old-skool" methods:
http://www.grymoire.com/Unix/index.html
http://www.shelldorado.com/

"All editors frustrate, but ed often amazes while others disappoint."
This is the easiest ref, and shows you the roots of all the other text filtering utilities:
http://plan9.bell-labs.com/magic/man2html/1/ed

You must have this (I still come back to it when I get stuck, just for ideas and thinking):
http://wolfram.schneider.org/bsd/7thEdManVol2/edtut/edtut.pdf

The POSIX Reference manpage (download this as well):
http://www.opengroup.org/onlinepubs/9699919799/utilities/ed.html

Tutorials:
http://wiki.bash-hackers.org/doku.php?id=howto:edit-ed
heh, I had an old form url, so looked up that page for the new form: that insertHead function is one of mine; that's how I script ed (with printf '%s\n').
http://sdf.lonestar.org/index.cgi?tutorials/ed
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum