1

I would like to execute a series of commands upon reboot, inside a SCREEN environment. I can do this via my crontab:

@reboot /usr/bin/screen -d -m -S myPseudoDaemon /path/to/something.sh

...so I could just put the commands in 'something.sh'. What complicates this case, is that the series of commands includes a sudo and a chroot:

sudo -i # become root
chroot /opt/debianUnstable # enter the chroot-ed Debian unstable
/bin/bash # source my bash setting, including mandatory env vars
/path/to/whatever.sh # the actual "daemon"

Any idea how I could do this?

2
  • Add yourself to the sudoers list. Commented Sep 20, 2013 at 8:12
  • @devnull: That is already done - passwordless sudo works, but opens the root shell and waits there. I want to run the rest of the commands inside it... Commented Sep 20, 2013 at 8:31

4 Answers 4

1

When you execture chroot, you will have an interactive shell or run command with special root directory, which means your root have already been in the new root.

First you'd execute the shell in new root,

chroot [OPTION] NEWROOT /bin/bash --login

From the bash manpage, we know that when bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. So you'd probably add /path/to/whatever.sh to profile script which is relative to the new root.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, your suggestion is the closest to a solution. But if I did that, the user would no longer be able to login normally - he would always be executing the script... Is there a way to both have the --login effect (reading the config files) and running my script?
0

Your /bin/bash is creating a shell. You should write :-

/bin/bash /path/to/whatever.sh # the actual "daemon"

2 Comments

If I do that, the env vars inside my .bashrc/.profile are not set. I run /bin/bash first to get to my 'default' profile.
Good point. you could 'source' the file. thegeekstuff.com/2010/07/execute-shell-script
0

Did you tried to schedule it in root crontab ?

It will allow you to chroot without password.

To run a command with your applicative user, you can use:

su -u <user> -c <command>

Comments

0

In the end, I solved this using the advise from jcb00s and Teg - but since I needed a specific part of my environment, I placed it in a script that executes in the chroot:

@reboot /usr/bin/screen -d -m -S autoLoginScreen /root/chrootPollAndBuildWithUnstable.sh

Inside /root/chrootPollAndBuildWithUnstable.sh:

# cat /root/chrootPollAndBuildWithUnstable.sh
chroot /opt/debianUnstable/ /pollAndBuildInsideChroot.sh

And inside the chroot-ed debian unstable filesystem, I do the final step...

# cat /opt/debianUnstable/pollAndBuildInsideChroot.sh 
su - <user> -c '/bin/bash /home/user/work/builder.sh'

...where the builder script sets the environment and proceeds with the build.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.