non-shell command running inside shell script - linux

I am trying to run a non shell script command inside a .sh script.
Current code looks like this :
#!/bin/bash
echo "Enter name of the folder you want to join!"
read folder
cd ~/domains/name/public_html/$folder/sites/default/
echo "enabling u7seven theme!"
drush en u7seven -y;
echo "disabling overlay!"
drush dis overlay -y;
echo "running all-folder script!"
u7d7up all-folder
Code that is not a shell code(which is local script somewhere on the server is ):
u7d7up all-folder
However, if I go and manually call this functionu7d7up all-folder from site root it works.
Since I am having more than 10 sites, i'd like to just call the script without entering and doing all these commands manually.

You might be facing an issue due to non-presence of u7d7up in the $PATH variable.
A robust way to write your script will be put the absolute path of the u7d7up; also you would need to check for the permissions on it.
#!/bin/bash
echo "Enter name of the folder you want to join!"
read folder
cd ~/domains/name/public_html/$folder/sites/default/
echo "enabling u7seven theme!"
drush en u7seven -y;
echo "disabling overlay!"
drush dis overlay -y;
echo "running all-folder script!"
/absolute-path-to/u7d7up /absolute-path-to/all-folder

Related

Can't write to file with script

I have written a script that is supposed to run some commands, reboot my ubuntu server and then after the server boots up, resume operation, and removes the entry from the bash file.
I have been trying for hours to figure out why my script won't write to .bashrc (or any file for that matter). Testing the commands individually works just fine. However, when run through my script nothing is written to the file. At this point I am stumped and would really like a fresh pair of eyes to help as I am sure the reason is silly and probably something I am missing. This would be my first script so sorry if this ends up being a stupid question. I put the "sudo reboot command in a comment so I won't have to reboot each time.
The script's name is test.sh and is run from ~/ . I hope this is clear and I didn't miss anything.
#!/bin/bash
echo "Script initiating"
#condition for script to run after reboot, created later on
if [ ! -f /var/run/bootflag ]; then
echo "First run"
script="bash ~/test.sh"
#this will add the script in the bash file so it will be ran on next boot
echo "$script" >> ~/.bashrc
echo "bash entry added"
#creating flag file to check if this is a second run
sudo touch /var/run/bootflag
echo "Flag created"
echo "Rebooting..."
#sudo reboot
else
echo "resuming script..."
echo "cleaning up..."
#remove the bash entry by replacing it with a space
sed -i '/bash/d' ~/.bashrc
echo "bash entry removed"
#remove the boot flag
sudo rm -f /var/run/bootflag
echo "bootflag removed"
echo "running commands post-reboot"
#commands here
echo "script exiting"
fi
I finally figured it out.
Because I was running the script with sudo, the script would take the relative path ~/.bashrc for the root user. The script was working fine all along, I was just looking at my user's .bashrc and was expecting things to appear, when all along the root user's .bashrc file was being written in.
I used sudo inside the script for all elevated commands and run the script normally without sudo and there it is, working as expected.
As I thought, this was beyond silly after all. At least I learned something today.
Thank you all for taking the time to reply and help, I really appreciate it.

Is it possible to auto reboot for 5 loops through mint?

I am currently using the following command to run reboot
sudo shutdown -r now
however, I would need to run it for 5 loops before and after executing some other programs. Was wondering if it is possible to do it in MINT environment?
First a disclaimer: I haven't tried this because I don't want to reboot my machine right now...
Anyway, the idea is to make a script that can track it's iteration progress to a file as #david-c-rankin suggested. This bash script could look like this (I did test this):
#!/bin/sh
ITERATIONS="5"
TRACKING_FILE="/path/to/bootloop.txt"
touch "$TRACKING_FILE"
N=$(cat "$TRACKING_FILE" | wc -c)
if [ "$N" -lt "$ITERATIONS" ]; then
printf "." >> "$TRACKING_FILE"
echo "rebooting (iteration $N)"
# TODO: this is where you put the reboot command
# and anything you want to run before rebooting each time
else
rm "$TRACKING_FILE"
# TODO: other commands to resume anything required
fi
Then add a call to this script somewhere where it will be run on boot. eg. cron (#reboot) or systemd. Don't forget to remove it from a startup/boot command when you're finished or next time you reboot, it will reboot N times.
Not sure exactly how you are planning on using it, but the general workflow would look like:
save script to /path/to/reboot_five_times.sh
add script to run on boot (cron, etc.)
do stuff (manually or in a script)
call the script
computer reboots 5 times
anything in the second TODO section of the script is then run
go back to step 3, or if finished remove from cron/systemd so it won't reboot when you don't want it to.
First create a text document wherever you want,I created one on Desktop,
Then use this file as a physical counter and write a daemon file to run things at startup
For example:
#!/bin/sh
var=$(cat a.txt)
echo "$var"
if [ "$var" != 5 ]
then
var=$((var+1))
echo "$var" > a.txt
echo "restart here"
sudo shutdown -r now
else
echo "stop restart"
echo 0 > a.txt
fi
Hope this helps
I found a way to create a file at startup for my reboot script. I incorporated it with the answers provided by swalladge and also shubh. Thank you so much!
#!/bin/bash
#testing making a startup application
echo "
[Desktop Entry]
Type=Application
Exec=notify-send success
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_CA]=This is a Test
Name=This is a Test
Comment[en_CA]=
Comment=" > ~/.config/autostart/test.desktop
I create a /etc/rc.local file to execute user3089519's script, and this works for my case. (And for bootloop.txt, I put it here: /usr/local/share/bootloop.txt )
First: sudo nano /etc/rc.local
Then edit this:
#!/bin/bash
#TODO: things you want to execute when system boot.
/path/to/reboot_five_times.sh
exit 0
Then it works.
Don't forget edit /etc/rc.local and remove /path/to/reboot_five_times.sh when you done reboot cycling.

Why doesn't $(which node) work inside a cron job executing a BASH shell?

According to this answer the reason cron doesn't have access to environment variables normally associated with a BASH terminal is because it doesn't source the users .bashrc file.
I have a script which does source my .bashrc file but it still fails to find my currently in use version of Node (meaning I need to list the full directory and change it with every update!).
Script:
#!/bin/bash
source $HOME/.bashrc # <-- even after sourcing .bashrc, '$(which node)' returns nothing
NODE="$(which node)" # <-- output is blank in cron job
PROCESS="/home/grayedfox/.nvm/versions/node/v8.9.4/bin/node /home/grayedfox/github/blockscrape/main.js"
LOGFILE="/tmp/log.out"
export BLOCKSCRAPECLI="/opt/litecoin-0.14.2/bin/litecoin-cli"
if pgrep -f "$PROCESS" > /dev/null; then
echo "Blockscrape is doing it's thing - moving on..." >> $LOGFILE
else
echo "Blockscrape not running! Starting again..." >> $LOGFILE
echo "Process: $PROCESS" >> $LOGFILE
echo "Node: $NODE" >> $LOGFILE # <-- outputs only 'Node: ' in log file
$PROCESS >> $LOGFILE
fi
Crontab:
# make default shell BASH
SHELL=/bin/bash
# reboots litecoin daemon if it dies
#reboot /opt/litecoin-0.14.2/bin/litecoind
# check every minute to see if block scrape running and restart it if not
* * * * * /home/grayedfox/github/blockscrape/restartBlockscrape.sh
I can confirm that node (and doing "which node") works fine in my terminal.
Thanks for the help!
As discussed in this answer - thethis answer problem was that in Ubuntu 16.04 (and many prior versions) the standard .bashrc file quits if it's not being run interactively.
Commenting out the below code from my bash file fixed it:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac

How to run npm or angular 2 ng serve command from Bash Script?

I am looking to create an interactive bash script for our development team (never did this before) as our project has various needs and due to future growth on the team I want this to be easy as pi.
Normally for my work on the team I would change directory to the angular folder and run ng serve which kicks off the frontend development server. How do I do that in a bash script?
So far I have this:
#!/bin/bash
echo -e "Hello, "$USER".\nWelcome to the BICE build script!\nVersion 0.1"
echo -e "Which build option do you wish to use?\n1. Frontend (default)\n2. Backend\n3. Database\n4. Production"
echo -n "Enter your choice [1-4] or press [ENTER]:"
read choice
if [choice == 1]; then
echo "Option 1 selected"
cd /angular
#call ng serve
Thanks!
/angular seems weird as a working directory but if angular is a subdirectory of the one holding the script a
cd ./angular
ng serve
cd ..
should work...
Maybe you could look at this question so you can call the script from any working directory.
Also note that you are missing a fi at the end of your if, there sould be spaces after opening and before closing bracket and choice should be referenced with a $ sign:
#!/bin/bash
echo -e "Hello, "$USER".\nWelcome to the BICE build script!\nVersion 0.1"
echo -e "Which build option do you wish to use?\n1. Frontend (default)\n2. Backend\n3. Database\n4. Production"
echo -n "Enter your choice [1-4] or press [ENTER]:"
read choice
if [ $choice == 1 ]; then
echo "Option 1 selected"
cd ./angular
ng serve
cd ..
fi

How to execute a script when xfce session ends

Is it possible to run a script/command when the xfce session stops ?
See http://mail.xfce.org/pipermail/xfce/2012-November/031694.html - There, Erik Habicht suggested creating a wrapper script in /usr/local/bin/xfce4-session (or another dir that precedes the dir where xfce4-session is installed, /usr/bin in your PATH). This way, you do not have to change /usr/bin/X11/xfce4-session, so it can be updated independently.
#!/bin/bash
# Add your own pre-session logic here
/usr/bin/xfce4-session
# Add your own logout logic here
then
$ chmod +x /usr/local/bin/xfce4-session
It's not perfect (depends on PATH order) but may be more palatable.
(Note: I promoted my comment to an answer.)
Change the /usr/bin/xfce4-session executable with a shell script which runs the original xfce4-session and your logout script if xfce4-session finished.
# mv /usr/bin/xfce4-session /usr/bin/xfce4-session.orig
The new /usr/bin/xfce4-session file:
#!/bin/bash
/usr/bin/xfce4-session.orig
echo "my logout script" > /tmp/testfile
Don't forget to set the execute permissions:
# chmod a+x /usr/bin/xfce4-session
(Tested on Debian Squeeze.)
I'd prefer solution that does not touch system directories or files and will run the logout hook within current user session and its privilledges.
below is my solution:
create ~/.local/bin/xfce4-session-logout script with the following contents:
#!/bin/bash
PRELOGOUT=${HOME}/scripts/pre-logout.sh
RESULT=RES_`echo -e "logout\nrestart\nshutdown\nsuspend" | zenity --height=250 --list --title "Logout from $USER" --column "What do You want to do?"`
case $RESULT in
RES_logout)
[ -x $PRELOGOUT] && $PRELOGOUT
/usr/bin/xfce4-session-logout --fast --logout
;;
RES_restart)
[ -x $PRELOGOUT] && $PRELOGOUT
/usr/bin/xfce4-session-logout --fast --reboot
;;
RES_shutdown)
[ -x $PRELOGOUT] && $PRELOGOUT
/usr/bin/xfce4-session-logout --fast --halt
;;
RES_suspend)
/usr/bin/xfce4-session-logout --suspend
;;
*)
exit 1
;;
esac
and make it executable:
chmod u+x ~/.local/bin/xfce4-session-logout
Now, put whatever You need to be executed at logout action to ~/scripts/pre-logout.sh and make it executable
chmod u+x ~/scripts/pre-logout.sh
after relogin, either menu > logout button or Alt+f3: "logout" will bring simple dialog for leaving the current session
Note: pressing Alt+F4 does not work with it, but maybe some black belted xfce4 users will provide some suggestion
I validated the answer above since it does not involve new code writing.
I however found another way to proceed : create a X11 program which would be launched at session startup : it could execute custom scripts when the X session is closed
Note : the drawback is that the used scripts could not connect to X windows so this solution may, depending on the need, execute the script too late.

Resources