Linux startup init.d file always fails with following bash, whats wrong? - linux

I have the following bash script to update to my website my current ip. It works fine stand alone, but put into a startup file, fails upon startup. I'm guessing it's a sequencing thing, but I'm not sure how to fix the sequencing, and after a few hours of googling and trying everything I can think of, I'm hoping someone can lead me in the right direction! This is what I am trying to run:
#!/bin/sh
IP_ADDR=$(ifconfig eth0 | sed -rn 's/^.*inet addr:(([0-9]+\.){3}[0-9]+).*$/\1/p')
wget -q -O /dev/null http://example.com/private/RPi_IP.php?send=${IP_ADDR}
I can't figure out what to do. I tried adding it to other startup programs even, and it fails upon startup too. I'm using a Raspberry Pi. Any ideas?

Your path might not be what you expect. You should fully-qualify any commands that you use. Especially for programs that live in /sbin/
ie
/sbin/ifconfig
/usr/bin/sed
/usr/bin/wget

Related

at job scheduler doesn't work on my Ubuntu

I know there are many linux experts here, I wish to get little help with at command in Ubuntu.
I have been troubled by at command in ubuntu (18.04 and 20.04) for quite a while, but I don't know where I made a mistake. I've tried at on three of my Ubuntu systems and it doesn't work on any of them. at is very handle and nice job scheduler, I really want to get it to work so that I do not have to manually launch programs in the late night on a shared Ubuntu server. I read many tutorials on at command, here is a very good one.
at now + 1 minutes -f ~/myscript.sh, it looks really great and can save me lots of energy. Unfortunately, when myscript.sh is extremely simple,then at now + 1 minutes -f ~/myscript.sh can run smoothly and I get what I expected. Here is everything I have in myscript.sh:
echo $(date) > ~/Desktop/time.txt
On top of that, it never worked for me. For example when I change myscript.sh to
echo $(date) > ~/Desktop/time.txt
pycharm.sh
Basically what myscript.sh does it is noting down the time and to open Pycharm IDE. I can run sh myscript.sh without at , it wroks very well. However, when I run at at now + 1 minutes -f ~/myscript.sh, the time is noted down but Pycharm was not never opened (I can see the process in htop if Pycharm is open). Also at now + 1 minutes -f ~/script.sh does not work with any of my other shell scripts.
Could you please help me understand where I have done wrong and how to make it work. Thank you very much.
PyCharm and other GUI programs need a lot of information from your environment. The atd daemon which runs jobs for at does not have access to this environment. You will need to specify it directly.
I recommend running printenv redirected to a file in an at job. Then compare that to printenv running from a terminal in your GUI session. Find the differences and see if you can set them up the same way at the beginning of your at script.

How to use "tee" with "source" command in Linux?

On Linux I'm using "tee" to capture the output of "source" command and print it to output log file, but failed. The command I'm using is like this:
source ./my_run.sh 2>&1 | tee -i my_run_log
The intention of my_run.sh is to "make" some compile job, as well as some routine jobs like cd, rm and svn update. The content of my_run.sh is like follows:
make clean
cd ..
rm ./xxx
svn up -r 166
cd ./aaa/
sed -i -e ......
make compile
make run
However, when I run it the "tee" just does NOT work, and do NOT give me the log file at all. In order to verify that the entire environment is good, I did a simpler test with:
ll 2>&1 | tee -i log
and in this simpler scenario the "tee" works perfectly fine and prints out "log" as I expected.
Can anyone help me find out where my problem is?
btw,
I'm working on Red Hat Linux (Release 5.9), using bash shell.
Thanks in advance!
SOME MORE COMMENTS:
I did some more tests and found that as long as the my_run.sh script has got "make xxx" stuffs in it, then "tee" will fail. Seems like tee does NOT like make. Any solutions?
Problem solved; many thanks goes to #thatotherguy in leading me to the solution. The log output was actually deleted by the make clean process. After fixing the clean stuff in the makefile, everything is good.

Why does my crontab not work?

I am planning to run some bash scripts every minute, and I wrote:
* * * * * bash ~/Dropbox/temp_scripts/run_all_scripts
in crontab.
It was supposed to run every minute, but it did not work. Does anyone have idea why this happens?
Transferring a comment into an answer.
Add I/O redirection to the command line in the crontab entry:
>/tmp/run_all_scripts.out 2>/tmp/run_all_scripts.err
Review the contents of the files after a minute or two has passed. Consider recording the environment to see if that's part of the problem. And consider using bash -x instead of just bash.
If you still don't get anything (the files in /tmp are not created), then you've got issues with cron; the daemon isn't running, or your user does not have permission to use it (but crontab isn't telling you that), or you've not submitted your crontab to the program (what does crontab -l say?), or … whatever is really wrong.
Note, too, that the output from cron jobs is normally (well, at least sometimes — on Mac OS X for a system I currently use, and Solaris for another that I've used previously) emailed to the person whose job it is. You should review the email on the system.
Thank you! I have already fixed it! The reason why it does not work is I used "ls -a .sh" in the script, and when the crontab did not find any *.sh files in the folder it was executing. When modifying it to "ls -a $HOME/Dropbox/temp_scripts/.sh", everything works! This debugging technique is quite helpful!
It is, in many ways, the most basic of debugging techniques — make sure you see what is actually happening. If you're not sure why a shell script isn't working, make sure you can see that it is executing and what it is producing in the way of output, and (very often) make sure you can see what it is executing with bash -x or equivalent. (AFAIK, all shells support -x to trace the execution.)

Script in a embedded linux

I believe that this is a silly question but is my first time in wich I do a script. I am working on Linux, in an embedded system, and I think that what I want to do is quite simple but for me is not working.
I need to set an ip, start the startx & server for graphical mode and give to my application permission and run it, so I try like this:
#!/bin/sh
#
#Start
#
echo "Start......"
ifconfig eth0 X.X.X.X
startx &
cd /home
chmod a+x myApplication
./myApplication
exit $?
And then I save my script like S80script and I put it in the /etc/init.d folder.
I ran it but after throw the startx server my application is not run.
How can I do this in a propertly way?
There is another way for do this?
Thank you so much and sorry because maybe it is a beginner question.
If your application need to acess the XServer than you need to export the DISPLAY environment variable.
Try to run the application using:
DISPLAY=:0 ./myApplication
I would suggest you to install (for learning purposes) Linux on your laptop, and become familiar with Linux and scripting on your laptop. Then, replace during debugging phase the first line #!/bin/sh with #!/bin/sh -vx or #!/bin/bash -vx if you can run that script in a terminal. You could also use logger(1) in your script. Read the Advanced Bash Scripting Guide even if it is imperfect.
startx is configurable (read the linked man page), and is starting some client applications (configured in e.g. /etc/X11/xinit/xinitrc or in $HOME/.xinitrc...); so you should start your $HOME/myApplication from that file.
BTW, you could invoke startx in some init script like /etc/rc.local or whatever is appropriate for your Linux distribution.
BTW, you almost certainly need a window manager (to be started after your backgrounded application, as the last command, probably in the same xinitrc....).
At last, your embedded Linux distribution probably has some other files and scripts to start the network. You should configure your network parameters appropriately (on Debian and related, you could do that in /etc/network/interfaces)

Loading Bar - Bash Script

I've seen code that shows a loading bar for copying and extracting files in a bash script. I've tried modifying it to work without luck. Is there a way to display an accurate and basic loading bar while OpenVPN loads/connects? Maybe even echo if there's a connection error? (Not as important) I need the output of starting OpenVPN to stay hidden.
openvpn --daemon --config /tmp/OpenVPN/config.ovpn >/dev/null 2>/dev/null
As long as your command doesn't offer a way to display its progress, there is no trivial way to implement this, as your bash script cannot know how far in its execution is it.
openvpn doesn't seem to offer such an option, so in this case, I don't think this would be possible without patching its source code and recompiling it.
If you want to display connection errors, you can use || though:
openvpn --daemon --config /tmp/OpenVPN/config.ovpn >/dev/null 2>/dev/null || echo "openvpn return with an error"

Resources