i want to run a script via crontab, but I get an error message and don't know how to fix it.
It works fine if I run it in the shell.
Code:
#$/bin/bash
DIR=/var/log
REC="foo#bar.com foo#bar.de"
TOTALFILES="$(/home/pi/scripts/count-files-dirs $DIR | cut -d' ' -f2)"
#echo "$TOTALFILES"
if (( "$TOTALFILES" > 36 )); then
echo -e "Subject:$(date +%A' '%d' '%B' '%G) PI-07 var/log status \n\n $(/usr/games/cowsay moh moh moh)\n" | sendmail $REC
else
true
fi
Error:
/home/pi/scripts/check-files: 12: /home/pi/scripts/check-files: 48: not found
I tried to find the issue, but I don't know where it is.
The first line in your script should be:
#!/bin/bash
Try to define the sendmail path. You can find the path with
'whereis sendmail'
almost like you did with cowsay.
The error indicates something in this file was not found, would need to see the content of this file to be able to tell (or make a guess).
/home/pi/scripts/check-files: 48: not found
Related
I'm making a script that document the system resources usage every certain interval.
But for some reason several commands give me this error:
myscript: /path/to/my/FILE is not a directory
These lines give me this error:
Input:
echo -e "Date: $(date +%H:%M:%S) /Hs/Mins/Segs" "\n" >> ~/Desktop/ShutDownTest/"$ExecutionNumber"/Ram/'Ram_Attempt_'"$RamAttempt".txt
echo -e 'The 3 most RAM usage processes:' "\n" "$(top -b -o %MEM -n 1 | head -n 10 | tail -n 4)" "\n" 2>> ~/Desktop/ShutDownTest/"$Execution_Number"/Errors.txt >> ~/Desktop/ShutDownTest/"$ExecutionNumber"/Ram/'Ram_Attempt_'"$RamAttempt".txt
echo -e "RAM usage:" "\n" 2>> ~/Desktop/ShutDownTest/"$ExecutionNumber"/Errors.txt >> ~/Desktop/ShutDownTest/"$ExecutionNumber"/Ram/'Ram_Attempt_'"$RamAttempt".txt
PD: $ExecutionNumber = 1 ; $RamAttempt is also equal to 1
I don't know why is this happening i executed all the 3 commands and redirect it to a file and all was all right.
Idk why i got this error or even what means, i mean i'm redirecting output to a file, of course that a file is not a directory, touch command also outputs the same error massage in some lines.
Any help is appreciated :)
I found out the problem, it was failing because instead of make a directory named as the variable $ExecutionNumber i made a file named as $ExecutionNumber.
I didn't realized it because i was expected something like "no such file or directory".
How curious, i thought that when you specify a path to a file or directory bash only evaluates if the last name is a file or a directory, supposing all the previous names should be directories.
I'll write it down later.
Thanks for the help #Barmar and #Gordon Davisson :)
I'm new to scripting.
I downloaded cygwin and Notepad++(I'm using unix option-for writing and saving the ".sh files")
I have below script
Below code is from command $ cat -v pinging.sh
#!/bin/bash
target=$1
# email report when
SUBJECT="Ping failed"
EMAILID="someemailid#gmail.com"
count=$( $SYSTEMROOT/system32/ping -n -c 1 $target | grep 'received')
if [ $count == 0 ];
then
echo "Host : $target is not Alive!! Try again later.. at $(date)" | mail -s "$SUBJECT" $EMAILID
else
echo "Yes! Host is Alive!"
fi
done
But my script is giving error -
$ ./pinging.sh www.google.com
./pinging.sh: line 9: [: ==: unary operator expected
Yes! Host is Alive!
./pinging.sh: line 17: syntax error near unexpected token `done'
./pinging.sh: line 17: `done'
I'm not sure what I am doing wrong here.
The script is giving error
I'm getting- "host is alive" message always even in case of destination unreachable messages too. If I'm using ping www.somesite.com and if I'm getting destination unreachable through cygwin or cmd, this code is giving host is alive.
I also tried if [ $count -et 0 ]; in above code
Please help me!
Best Regards,
The value of the $count variable is not a number. It is a full line of text.
When you expand it in the [ test (without quotes) it gets word-split by the shell and the contents of the [ test become invalid (too many words) and you get your error.
If you quote "$count" you will avoid the error (but still not get the results you want).
You need to filter out only the number from the ping output and then use that in your [ test.
Add set -x to the top of your script to see the commands that are actually being run and you'll see the problem.
I am new to bash and trying to write a script that disables kworker business as in aMaia's answer here.
So far, I have this, which I run from root:
1 #!/bin/bash
2
3 cd /sys/firmware/acpi/interrupts
4 for i in gpe[[:digit:]]* # Don't mess with gpe_all
5 do
6 num=`awk '{print $1}' $i`
7 if (( $num >= 1000 )); then # potential CPU hogs?
8 # Back it up and then disable it!!
9 cp $i /root/${i}.backup
10 echo "disable" > $i
11 fi
12 done
But running it results in:
./kkiller: line 10: echo: write error: Invalid argument
What is going on here? I thought $i was just the file name, which seems like the correct syntax for echo.
Suggestions for cleaning up/improving the script in general are also appreciated!
Update: With set -vx added to the top of the script, here is a problematic iteration:
+ for i in 'gpe[[:digit:]]*'
awk '{print $1}' $i
++ awk '{print $1}' gpe66
+ num=1024908
+ (( 1024908 >= 1000 ))
+ cp gpe66 /root/gpe66.backup
+ echo disable
./kkiller: line 10: echo: write error: Invalid argument
I had this problem too in Docker on Alpine linux environment. I think the problem is that echo by default put a newline character at the end of the string, and the kernel not accept it, but it is not the case in every system. In Docker I had this error, but the value was written despite the error message.
The solution (in Bash): echo -n disable >/sys/firmware/acpi/interrupts/gpe66. This way no newline is echoed.
Double-check all spelling. echo "disabled" will emit a write error even for root whereas echo "disable" succeeds.
I think it has something to with permissions. I don't think root has write access to those files by default. Try echoing manually 'disable' to that file, even as root you get the same error shown. So to make your script work, first do chmod 744 on $i before your echo, it should do the trick.
I got the same error message trying to disable a GPE that was already set to disabled:
# echo "disable" > /sys/firmware/acpi/interrupts/gpe17
-su: echo: write error: Invalid argument
# cat /sys/firmware/acpi/interrupts/gpe17
3718289 STS disabled unmasked
After enabling it, I could disable it without an error:
# echo "enable" > /sys/firmware/acpi/interrupts/gpe17
# echo "disable" > /sys/firmware/acpi/interrupts/gpe17
#
I have a file with a list of servers:
SERVERS.TXT:
192.168.0.100
192.168.0.101
192.168.0.102
From a gnome terminal script, I want open a new terminal, with a tab for each server.
Here is what I tried:
gnome-terminal --profile=TabProfile `while read SERVER ; do echo "--tab -e 'ssh usr#$SERVER'"; done < SERVERS.TXT`
Here is the error:
Failed to parse arguments: Argument to "--command/-e" is not a valid command: Text ended before matching quote was found for '. (The text was ''ssh')
Tried removing the space after the -e
gnome-terminal --profile=TabProfile `while read SERVER ; do echo "--tab -e'ssh usr#$SERVER'"; done < SERVERS.TXT`
And I get a similar error:
Failed to parse arguments: Argument to "--command/-e" is not a valid command: Text ended before matching quote was found for '. (The text was 'usr#192.168.0.100'')
Obviously there is a parsing error since the the shell is trying to be helpful by using the spaces to predict and place delimiters. The server file is changed without notice and many different sets of servers need to be looked at.
I found this question while searching for an answer to the issue the OP had, but my issue was a little different. I knew the list of servers, they where not in a file.
Anyway, the other solutions posted did not work for me, but the following script does work, and is what I use to get around the "--command/-e" is not a valid command" error.
The script should be very easy change to suit any need:
#!/bin/sh
# Open a terminal to each of the servers
#
# The list of servers
LIST="server1.info server2.info server3.info server4.info"
cmdssh=`which ssh`
for s in $LIST
do
title=`echo -n "${s}" | sed 's/^\(.\)/\U\1/'`
args="${args} --tab --title=\"$title\" --command=\"${cmdssh} ${s}.com\""
done
tmpfile=`mktemp`
echo "gnome-terminal${args}" > $tmpfile
chmod 744 $tmpfile
. $tmpfile
rm $tmpfile
Now the big question is why does this work when run from a file, but not from within a script. Sure, the issue is about the escaping of the --command part, but everything I tried failed unless exported to a temp file.
I would try something like:
$ while read SERVER;do echo -n "--tab -e 'ssh usr#$SERVER' "; \
done < SERVERS.txt | xargs gnome-terminal --profile=TabProfile
This is to avoid any interpretation that the shell could do of the parameters (anything starting with a dash).
Because it is concatenating strings (using -n), it is necessary to add an space between them.
Is this a problem of parsing command-line options? Sometimes if you have one command sending arguments to another command, the first can get confused. The convention is to use a -- like so:
echo -- "--tab -e 'ssh usr#$SERVER'";
Try to type
eval
before gnome terminal command.
it should be something like this:
eval /usr/bin/gnome-terminal $xargs
worked for me!
I am trying to write a script to track the progress of file change.
I have the following till now:
#!/bin/sh
old=‘ls -l /tmp/file‘
new=‘ls -l /tmp/file‘
while [ "$old" = "$new" ]
do
new=‘ls -l /tmp/file‘
done
echo "The file has been changed"
The above program when run gives the message:
new: command not found
Can someone please help.
Thanks
You probably have space around =.
In shell, when you assign the values you cannot put space around =:
MY_VAR = "my value" # this is wrong!
Shell will think: "call MY_VAR with arguments: ('=', 'my value') ", but wait! I don't know the command "MY_VAR"!
You need to do it this way:
MY_VAR="my value" # this is OK!
BTW, consider using inotifywatch command. Here's example:
inotifywatch -v -e access -e modify -t 60 -r /file/to/watch