When your script has a command to run in chroot environment,  script exits immediately after "chroot" command, reason was your file systems changes and script which was running is not available to file system any more after chroot.

But there will be times where you have to run commands in both normal and chroot environment, every one think it's tricky and how.... funny thing is it is not such difficult, we can do it using "chroot" command.

Lets see how.

Here we are not using chroot command and mounting, just we are using chroot and passing command s into a filesystem/drive which we want to chroot.

let's say /dev/sdb1 is where my other file system is, so usually we'll mount it and chroot into it. Now we are mounting /dev/sdb1 on  /mnt/my_file_system

So here is my part of my script

$ mkdir /mnt/my_file_system
$ mount /dev/sdb1 /mnt/my_file_system

now our drive which has file system is mounted now we'll pass commands into it. Before that very important thing is to mount /pro, becuse without mounting this file system you can't run any process.

$ chroot /mnt/my_file_system /bin/bash -c "mount proc proc /proc"
$ chroot /mnt/my_file_system /bin/bash -c "your command"

that simple, if you want to redirect your commands output to a file that is also possible.

$ chroot /chroot_dir /bin/bash -c "your command" > test.log 2>&1

enjoy scripting.