How can I run a script as root at startup ?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
oasipet
Level 1
Level 1
Posts: 2
Joined: Tue Mar 28, 2023 10:46 am

How can I run a script as root at startup ?

Post by oasipet »

Hi,

I have installed a binary distribution of ROOT (using root privileges).

I need to put the script

Code: Select all

source <dir-where-you-installed-root>/bin/thisroot.sh
near the end of the ~/.bash_profile (or ~/.login for csh/tcsh)
(see: https://redmine.jlab.org/projects/podd/ ... stallation),
to set up it.

In Mint Una, which file is to consider ? (root privileges are required)

Thanks in advance,
best regards
Last edited by LockBot on Thu Sep 28, 2023 10:00 pm, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29609
Joined: Wed Jul 06, 2011 3:58 am

Re: How can I run a script as root at startup ?

Post by xenopeek »

Nowhere in the linked page do the instructions say you should run this script as root. Are you sure you need to do that? Where does it say that. Red flags if it needs that for a shell script! Find an alternative way to install it that doesn't require you to run the shell script as root on start up.

On Linux Mint you have a ~/.profile file. Add the line (with the correct path) in that. This executes it as your user on log in.
Image
oasipet
Level 1
Level 1
Posts: 2
Joined: Tue Mar 28, 2023 10:46 am

Re: How can I run a script as root at startup ?

Post by oasipet »

Referring to https://redmine.jlab.org/projects/podd/ ... stribution
to configure the environment:

Code: Select all

# source ~/Apps/root/bin/thisroot.sh
works...

but I need root permissions, infact:

Code: Select all

$ source ~/Apps/root/bin/thisroot.sh
bash: /root/bin/thisroot.sh: File or directory does not exist
Instead if directory "root" is in the current user's home folder

Code: Select all

$ chmod 777 ~/root

Code: Select all

$ source ~/root/bin/thisroot.sh
works...

If I put near the end of the ~/.profile the above code:

Code: Select all

source ~/root/bin/thisroot.sh
it doesn't work.
Last edited by oasipet on Tue Mar 28, 2023 3:53 pm, edited 2 times in total.
User avatar
xenopeek
Level 25
Level 25
Posts: 29609
Joined: Wed Jul 06, 2011 3:58 am

Re: How can I run a script as root at startup ?

Post by xenopeek »

Let's go back to the beginning; how did you install ROOT? From the Ubuntu binary packages? Or did you build it from source. Either way, I don't see it requiring you to install this software in the home directory of the administrator user (directory /root). You can put the files in whichever directory you want. And then you use:
source <the directory where you extracted the .tar.gz>/root/bin/thisroot.sh

So if you extracted it in a directory called Apps in your own home directory /home/oasipet/ you would do:
source /home/oasipet/Apps/root/bin/thisroot.sh

If you put it in a system-wide directory /apps that you made, you would do:
source /apps/root/bin/thisroot.sh

As a regular user you are not permitted to access the administrator user's home directory, and you should not need to. Simply extract the .tar.gz in a more appropriate place.
Image
User avatar
xenopeek
Level 25
Level 25
Posts: 29609
Joined: Wed Jul 06, 2011 3:58 am

Re: How can I run a script as root at startup ?

Post by xenopeek »

oasipet wrote: Tue Mar 28, 2023 1:53 pm Instead if directory "root" is in the current user's home folder

Code: Select all

$ chmod 777 ~/root

Code: Select all

$ source ~/root/bin/thisroot.sh
works...

If I put near the end of the ~/.profile the above code:

Code: Select all

source ~/root/bin/thisroot.sh
it doesn't work.
The .profile file gets loaded when you log in. Did you log out / reboot after making the change to .profile?

Others, do you perhaps also have a .bash_profile file in your home directory? Then the .profile file doesn't get loaded, unless the .bash_profile file does that.
Image
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: How can I run a script as root at startup ?

Post by rene »

xenopeek wrote: Tue Mar 28, 2023 4:58 pm The .profile file gets loaded when you log in. Did you log out / reboot after making the change to .profile?
source is not POSIX and I upon reading assumed it would be a matter of /bin/sh (dash) rejecting it, but ~/.profile is explicitly sourced by in our case /usr/sbin/lightdm-session which is in fact a /bin/bash script and not a /bin/sh one so that didn't pan out.

Still posted, so you/he don't have to check, but also to say to poster that it's still a bit better to in a file such as ~/.profile use old-style . rather than "new-style" source so as to not have to know the above.
Locked

Return to “Scripts & Bash”