unrecognized service for my init.d - linux

here is my full code, I just use following code:
case "$1" in
st)
echo 450 > /sys/class/backlight/intel_backlight
;;
stop)
echo
;;
rst)
echo; echo 450 > /sys/class/backlight/intel_backlight
;;
*)
echo "Usage: $0 {st|stop|rst}"
exit 1
;;
esac
why it raise this error

the reason is my init.d file has not "x" permission, so I need first chmod 755 /etc/init.d/brightness before running it

You may want to consider base your init script on the skeleton which live in /etc/init.d. Your script is probably inpcomplete.

Related

Nested Case statement in shell script | Getting Syntax error

I am trying to run below shell script but i am getting syntax error.
script.sh env1 ManagedSvr1
line 29: warning: here-document at line 6 delimited by end-of-file (wanted `EOF')
line 30: syntax error: unexpected end of file
#!/bin/bash
case "$1" in "env1")
ssh weblogic#hostname1 << EOF
case "$server" in
"ManagedSvr1")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log
;;
"ManagedSvr2")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
;;
esac
;;
"env2")
ssh weblogic#hostname2 << EOF
case "$server" in
"ManagedSvr1")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log
;;
"ManagedSvr2")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
;;
esac
;;
esac
Your Here Docs specify EOF to end them (<< EOF) but you never have an EOF to end them. Note that EOF doesn't mean End of File, it means the string 'EOF'. https://en.wikipedia.org/wiki/Here_document has examples.
I'm not sure what you're hoping to accomplish, but it looks to me that you need to specify which file to tail. Are you hoping to pass the inner case into the remote shell on the server you're sshing into? It would simplify your code to set your filename and servername first and then ssh and execute the command. Actually, I don't see much purpose in your inner case statements anyway. Instead of wrapping everything in the 'env' case, you could just set the hostname to a variable. And then the "servername" can just be interpolated into the filesystem path. something like this seems like a simple approach:
#!/bin/bash
case "$1" in
env1)
hostname="hostname1"
;;
env2)
hostname="hostname2"
;;
esac
echo ssh weblogic#$hostname tailf /app/Oracle/Middleware/domains/dq/servers/$servername/logs/$servername.log
Seems to work simply and straight forward:
servername=ManagedSvr1 ./t.sh env1
ssh weblogic#hostname1 tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log
Take out the "echo" to actually execute the ssh.
You actually don't need "HERE" docs, per say, you're using one case statement which is taking standard input to your SSH command. take single quotes as input to ssh This lets you do multiline input in peace. Quoting your "EOF" and terminating it, is also acceptable usage.
#!/bin/bash
case "$1" in
"env1")
ssh -tt weblogic#hostname1 <<< '
case "$server" in
"ManagedSvr1")
cat /var/log/syslog
;;
"ManagedSvr2")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
;;
esac '
;;
"env2")
ssh -tt weblogic#hostname2 <<< '
case "$server" in
"ManagedSvr1")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log
;;
"ManagedSvr2")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
;;
esac '
;;
esac
This would be an easier way to manage the script. Especially as you're not utilising remote user change: Lets you set all your variables locally, and then just connect and execute the single command.
#!/bin/bash
server="$2"
case "$1" in
"env1")
hostname="hostname1"
case "$server" in
"ManagedSvr1")
remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr1.log)
;;
"ManagedSvr2")
remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log)
;;
esac
;;
"env2")
hostname="hostname2"
case "$server" in
"ManagedSvr1")
remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log)
;;
"ManagedSvr2")
remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log)
;;
esac
;;
*)
exit 1
;;
esac
if [[ $? == 0 ]]
then ssh weblogic#$hostname $remote_command
fi

if statement not working in cron

I have the following code: (record.sh)
cd $(dirname $0)
dt=$(date '+%d/%m/%Y %H:%M:%S');
echo $dt;
read action < /home/nfs/sauger/web/pi/action.txt
echo $action;
if [[ $action == *"start"* ]]
then
echo "start recording"
./gone.sh
exit 1
elif [[ $action == *"stop"* ]]
then
echo "stop recording"
./gone.sh
exit 1
else
#More stuff done here
fi
When I run this script manually the output is the following:
19/01/2016 19:07:11
start
start recording
If the same script is run via a (root) cronjob, the output is the following:
19/01/2016 19:07:01
start
As you can see, the file "action.txt" has been read without a problem ("start" is logged both times) so this should not be an issue of permissions or wrong paths. But when run as a cronjob, the if-statement is not called. No "start recording" appears.
So my question is: Why does the if-statement work when I call the script manually, but not when this is done via cron?
Your script is written for bash; these errors are almost certainly indicative of it being run with /bin/sh instead.
Either add an appropriate shebang and ensure that it's being called in a way that honors it (/path/to/script rather than sh /path/to/script), or fix it to be compatible. For instance:
case $action in
*start*)
echo "start recording"
./gone.sh
exit 1
;;
*stop*)
echo "stop recording"
./gone.sh
exit 1
;;
esac

My custom init shell script stops deployment script

I have a deployment script which works fine. At the end of it I added this block:
cat << EOF > /etc/init.d/uwsgi
#!/bin/bash
daemon=$APPVENV/bin/uwsgi
args="--emperor $APPCONF/uwsgi/app.ini --daemonize /var/log/emperor.log --emperor-pidfile $APPDIR/emperor.pid --gid `id -g $APPUSER`"
pid=$APPDIR/emperor.pid
case "$1" in
start)
echo "Starting uwsgi"
start-stop-daemon -p $pid --start --exec $daemon -- $args
;;
stop)
echo "Stopping script uwsgi"
start-stop-daemon --signal INT -p $pid --stop $daemon -- $args
;;
reload)
echo "Reloading conf"
kill -HUP $(cat $pid)
;;
*)
echo "Usage: /etc/init.d/uwsgi {start|stop|reload}"
exit 1
;;
esac
exit 0
EOF
Now when I run my deployment script it gets stuck here, and I just see a blinking cursor and it doesn't run any lines after it.
Have I done anything wrong with my formatting, as I know creating a file with cat was something very picky about how it was formatted, tabulated etc.
variables are still substituted in heredocs, also subshelling with $( ), which you do in your script here:
kill -HUP $(cat $pid)
your installer attempts to cat that file, with $pid probably empty, therefore it waits for input from standard input.
You want to escape those "$" to prevent expansion, like
\$(cat \$foo)
And of course with all those variables you don't want to get expanded during installation too.

Python: How to wait until disk activity subsides?

Hegle Jensens' wrote a great SnapBtr script that makes snapshot-based backups with smart algorithm that chooses which old backup(s) to delete, when the free space become scarce.
Unfortunately the BTRFS file system has a peculiarity that after any delete command it doesn't immediately free the disk space; instead it merely schedules the deletion of each node. The actual process of freeing the disk space happens in the background, and only after it is finished, we know how much free space became available.
That's why I'd like to improve this script so after deletion of a spare subvolume it will wait until there will be no hard drive activity in order to get the actual free disk space statistics.
The question: knowing that there is so many Python libraries around, do you know any that would return something, that I can use to get hard drive activity saturation in %?
If that helps, I've already made a Bash script wait-for-disk-idle.sh, which relies on iostat for the disk activity information. But I guess that calling external Bash process for something so simple is quite inefficient and error prone (what if the iostat is not installed?):
#! /bin/bash
USAGE="Usage: `basename $0` [-t sample time] [-p disk IO percent threshold] disk-device"
time=4
percent=10
# Parse command line options.
while getopts ":t:" OPT; do
case "$OPT" in
t)
time=$OPTARG
;;
:)
# getopts issues an error message
echo "`basename $0` version 0.1"
echo $USAGE >&2
exit 1
;;
\?)
# getopts issues an error message
echo "`basename $0` version 0.1"
echo $USAGE >&2
exit 1
;;
esac
done
while getopts ":p:" OPT; do
case "$OPT" in
p)
percent=$OPTARG
;;
:)
;;
\?)
# getopts issues an error message
echo "`basename $0` version 0.1"
echo $USAGE >&2
exit 1
;;
esac
done
# Remove the switches we parsed above.
shift `expr $OPTIND - 1`
# We want at least one non-option argument.
# Remove this block if you don't need it.
if [ $# -eq 0 ]; then
# getopts issues an error message
echo "`basename $0` version 0.1"
echo $USAGE >&2
exit 1
fi
echo percent: $percent, time: $time, disk: $1
while [[ $(iostat -d -x $time 2 $1 |
sed -n 's/.*[^0-9]\([0-9][0-9]*\)[\.,][^,^\.]*$/\1/p' | tail -1) > $percent
]]; do
echo wait
done
Here is an answer I've got from the (former) maintainer of the script:
I am no longer cleaning up using the SnapBtr.py script, however you
may be able to wait for the delete to complete with a btrfs filesystem sync.

Case statement not working in my shell script

I'm trying to write a shell script for Mac OSX Mountain Lion (10.8.2). However, no matter what I try, I can't get the switch statement to work. He is a very simplified version of the script:
#!/bin/bash
while getopts "ei" opt; do
case $opt in
e)
echo "Got option e"
;;
i)
echo "Got option i"
;;
\?)
echo "Invalid option: -$opt"
;;
esac
done
No matter what I try, I don't get any output. Any ideas? I've done scripts like this in the past.
You might be passing the options incorrectly don't forget the -
script.sh -e
Note you should also exit after an invalid option:
\?)
echo "Invalid option: -$opt"
exit 1
;;
Try replacing
\?)
echo "Invalid option: -$opt"
;;
with:
*)
echo "Invalid option: -$opt"
;;

Resources