How to I properly prevent systemd suspend using a script in /lib/systemd/system-sleep/ - linux

I'm fairly new to Linux and trying to learn. I'm using Plex Media Server and I'm trying to prevent the system from sleeping while streaming a file. I've searched the internet over the last few days and none of the solutions seem to work. One solution I feel is almost getting me there, but it's not quite working. Here is the script I've placed (and made executable) in /lib/systemd/system-sleep/ (based on this site).
#!/bin/bash
PATH=/sbin:/usr/sbin:/bin:/usr/bin
X_DISPLAY_USERNAME=myusername
plexresume()
{
number_of_sessions=$(curl -s localhost:32400/status/sessions? | sed -n "s/.*MediaContainer size=\"\(.*\)\".*/\1/p")
if [ ${number_of_sessions} -gt 0 ]; then
echo "[$(date +"%Y.%m.%d-%T")] Number of streamers = ${number_of_sessions} Plex session active, cancel suspend" >> /tmp/plex_sleep_log
return 1
else
echo "[$(date +"%Y.%m.%d-%T")] Number of streamers = ${number_of_sessions} Plex session -IN-active, going to sleep now zzzzzzzz........" >> /tmp/plex_sleep_log
return 0
fi
}
plexkeepalive()
{
echo "[$(date +"%Y.%m.%d-%T")] Resuming!!..." >> /tmp/plex_sleep_log
su ${X_DISPLAY_USERNAME} -c "DISPLAY=:0 /usr/bin/xdotool getmouselocation | grep 'x:1 y:1 '" > /dev/null
if [ "$?" == "0" ]; then
su ${X_DISPLAY_USERNAME} -c "DISPLAY=:0 /usr/bin/xdotool mousemove 9 9"
else
su ${X_DISPLAY_USERNAME} -c "DISPLAY=:0 /usr/bin/xdotool mousemove 1 1"
fi
return 0
}
case "$1" in
pre)
plexresume
;;
post)
plexkeepalive
;;
esac
I know it's executing because it's printing to the log file. But even when it's printing that Plex is active, it's still suspending the system. I've manually run the script using sudo outside of systemd and checking the value of $? after, which is 1.
When I use
journalctl -b -u systemd-suspend.service
I see the following:
Sep 26 12:47:03 systemname systemd[1]: Starting System Suspend...
Sep 26 12:47:03 systemname [141890]: /usr/lib/systemd/system-sleep/plexkeepalive failed with exit status 1.
Sep 26 12:47:03 systemname systemd-sleep[141887]: Entering sleep state 'suspend'...
One time I got a successful result, but I'm not sure how it happened:
Sep 26 10:51:45 systemname systemd-sleep[112491]: Entering sleep state 'suspend'...
Sep 26 10:52:25 systemname systemd-sleep[112491]: Failed to put system to sleep. System resumed again: Device or resource busy
Sep 26 10:52:25 systemname su[112556]: (to myusername) root on none
Sep 26 10:52:25 systemname su[112556]: pam_unix(su:session): session opened for user myusername(uid=1000) by (uid=0)
Sep 26 10:52:25 systemname su[112556]: pam_unix(su:session): session closed for user myusername
Sep 26 10:52:25 systemname su[112592]: (to myusername) root on none
Sep 26 10:52:25 systemname su[112592]: pam_unix(su:session): session opened for user myusername(uid=1000) by (uid=0)
Sep 26 10:52:25 systemname su[112592]: pam_unix(su:session): session closed for user myusername
Sep 26 10:52:25 systemname systemd[1]: systemd-suspend.service: Main process exited, code=exited, status=1/FAILURE
Sep 26 10:52:25 systemname systemd[1]: systemd-suspend.service: Failed with result 'exit-code'.
Sep 26 10:52:25 systemname systemd[1]: Failed to start System Suspend.
Sep 26 10:52:25 systemname systemd[1]: systemd-suspend.service: Consumed 2.732s CPU time.
Any help on this issue would be appreciated. I don't understand why returning 1 from the script is not preventing systemd-suspend.service from running. Thank you!

Related

Unable to run cron job with standard user account

The same job can be executed as root, but it can't execute as a standard user.
Is it permission problem or I need to change anything, I have no idea on it.
Thanks
SunOS 5.10 Generic_150400-30 sun4v sparc SUNW,SPARC-Enterprise-T5120
Command:
1) login as a root
2) crontab -l
* * * * * /usr/bin/date > /tmp/root.log
3) /tmp/root.log is here
1) login as a Non-root user
2) crontab -l
* * * * * /usr/bin/date > /tmp/non-root.log
3) /tmp/non-root.log is not here
The following permissions are OK for the binary file date
-bash-3.2# ls -l /usr/bin/date
-r-xr-xr-x 1 root bin 11056 Jan 22 2005 /usr/bin/date
-bash-3.2#
If the permissions are OK check your cron log on /var/cron/log file
-bash-3.2# tail /var/cron/log
< root 24592 c Fri Oct 20 18:50:21 2017
> CMD: /usr/bin/date > /tmp/non-root.log
> user 25192 c Fri Oct 20 18:51:00 2017
< user 25192 c Fri Oct 20 18:51:00 2017
> CMD: /scripts/collectdata.sh > /dev/null 2>&1
> root 25769 c Fri Oct 20 18:52:00 2017
< root 25769 c Fri Oct 20 18:52:00 2017
> CMD: /scripts/collectdata.sh > /dev/null 2>&1
> root 26853 c Fri Oct 20 18:54:00 2017
< root 26853 c Fri Oct 20 18:54:00 2017
-bash-3.2#
Thanks all, I finally found out the issue.
The reason is that non-root account is locked out, I think it maybe someone did many failure attempt which make this locked.
After I passwd -u "Account", the job can be run as expected. Thanks~

Crontab throwing error

I have the following content in crontab:
20 1,7,13,18 * * * /usr/sbin/automysqlbackup
15 * * * * root find /opt/activeMq/activemq-data/localhost/KahaDB/ -mtime +10 -type f -delete
but I get this when I restart cron service:
Feb 20 08:43:27 . crontab[14584]: (root) BEGIN EDIT (root)
Feb 20 08:44:08 . crontab[14584]: (root) REPLACE (root)
Feb 20 08:44:08 . crontab[14584]: (root) END EDIT (root)
Feb 20 08:44:28 . kernel: [325740.483115] init: cron main process (14563) killed by TERM signal
Feb 20 08:44:28 . cron[14641]: (CRON) INFO (pidfile fd = 3)
Feb 20 08:44:28 . cron[14642]: (CRON) STARTUP (fork ok)
Feb 20 08:44:28 . cron[14642]: Error: bad username; while reading /etc/crontab
Feb 20 08:44:28 . cron[14642]: (*system*) ERROR (Syntax error, this crontab file will be ignored)
Feb 20 08:44:28 . cron[14642]: (CRON) INFO (Skipping #reboot jobs -- not system startup)
I found the solution. Username field is only available at system level cron(etc/crontab) and not user level cron.

How to monitor newly created file in a directory with bash?

I have a log directory that consists of bunch of log files, one log file is created once an system event has happened. I want to write an oneline bash script that always monitors the file list and display the content of the newly created file on the terminal. Here is what it looks like:
Currently, all I have is to display the content of the whole directory:
for f in *; do cat $f; done
It lacks the monitoring feature that I wanted. One limitation of my system is that I do not have watch command. I also don't have any package manager to install fancy tools. Raw BSD is all I have. I do have tail, I was thinking of something like tail -F $(ls) but this tails each file instead of the file list.
In summary, I want to modify my script such that I can monitor the content of all newly created files.
First approach - use a hidden file in you dir (in my example it has a name .watch). Then you one-liner might look like:
for f in $(find . -type f -newer .watch); do cat $f; done; touch .watch
Second approach - use inotify-tools: https://unix.stackexchange.com/questions/273556/when-a-particular-file-arrives-then-execute-a-procedure-using-shell-script/273563#273563
You can cram it into a one-liner if you want, but I'd recommend just running the script in the background:
#!/bin/bash
[ ! -d "$1" ] && {
printf "error: argument is not a valid directory to monitory.\n"
exit 1
}
while :; fname="$1/$(inotifywait -q -e modify -e create --format '%f' "$1")"; do
cat "$fname"
done
Which will watch the directory given as the first argument, and cat any new or changed file in that directory. Example:
$ bash watchdir.sh my_logdir &
Which will then cat new or changed files in my_logdir.
Using inotifywait in monitor mode
First this little demo:
Open one terminal and run this:
ext=(php css other)
while :;do
subname=''
((RANDOM%10))||printf -v subname -- "-%04x" $RANDOM
date >/tmp/test$subname.${ext[RANDOM%3]}
sleep 1
done
This will create randomly files named /tmp/test.php, /tmp/test.css and /tmp/test.other, but randomly (approx 1 time / 10), the name will be /tmp/test-XXXX.[css|php|other] where XXXX is an hexadecimal random number.
Open another terminal and run this:
waitPaths=(/{home,tmp})
while read file ;do
if [ "$file" ] &&
( [ -z "${file##*.php}" ] || [ -z "${file##*.css}" ] ) ;then
(($(stat -c %Y-%X $file)))||echo -n new
echo file: $file, content:
cat $file
fi
done < <(
inotifywait -qme close_write --format %w%f ${waitPaths[*]}
)
This may produce something like:
file: /tmp/test.css, content:
Tue Apr 26 18:53:19 CEST 2016
file: /tmp/test.php, content:
Tue Apr 26 18:53:21 CEST 2016
file: /tmp/test.php, content:
Tue Apr 26 18:53:23 CEST 2016
file: /tmp/test.css, content:
Tue Apr 26 18:53:25 CEST 2016
file: /tmp/test.php, content:
Tue Apr 26 18:53:27 CEST 2016
newfile: /tmp/test-420b.php, content:
Tue Apr 26 18:53:28 CEST 2016
file: /tmp/test.php, content:
Tue Apr 26 18:53:29 CEST 2016
file: /tmp/test.php, content:
Tue Apr 26 18:53:30 CEST 2016
file: /tmp/test.php, content:
Tue Apr 26 18:53:31 CEST 2016
Some explanation:
waitPaths=(/{home,tmp}) could be written waitPaths=(/home /tmp) or for only one directory: waitPaths=/var/log
if condition search for filenames matching *.php or *.css
(($(stat -c %Y-%X $file)))||echo -n new will compare creation and modification time.
inotifywait
-q to stay quiet (don't print more then required)
-m for monitor mode: Command don't termine, but print each matching event.
-e close_write react only to specified kind of event.
-f %w%f Output format: path/file
Another way:
There is a more sophisticated sample:
Listenning for two kind of events (CLOSE_WRITE | CREATE)
Using a list of new files flags for knowing which files are new when CLOSE_WRITE event occur.
In second console, hit Ctrl+C, or in new terminal, tris this:
waitPaths=(/{home,tmp})
declare -A newFiles
while read path event file; do
if [ "$file" ] && ( [ -z "${file##*.php}" ] || [ -z "${file##*.css}" ] ); then
if [ "$event" ] && [ -z "${event//*CREATE*}" ]; then
newFiles[$file]=1
else
if [ "${newFiles[$file]}" ]; then
unset newFiles[$file]
echo NewFile: $file, content:
sed 's/^/>+ /' $file
else
echo file: $file, content:
sed 's/^/> /' $path/$file
fi
fi
fi
done < <(inotifywait -qme close_write -e create ${waitPaths[*]})
May produce something like:
file: test.css, content:
> Tue Apr 26 22:16:02 CEST 2016
file: test.php, content:
> Tue Apr 26 22:16:03 CEST 2016
NewFile: test-349b.css, content:
>+ Tue Apr 26 22:16:05 CEST 2016
file: test.css, content:
> Tue Apr 26 22:16:08 CEST 2016
file: test.css, content:
> Tue Apr 26 22:16:10 CEST 2016
file: test.css, content:
> Tue Apr 26 22:16:13 CEST 2016
Watching for new files AND new lines in old files, using bash
There is another solution by using some bashisms like associative arrays:
Sample:
wpath=/var/log
while : ;do
while read -a crtfile ;do
if [ "${crtfile:0:1}" = "-" ] &&
[ "${crtfile[8]##*.}" != "gz" ] &&
[ "${files[${crtfile[8]}]:-0}" -lt ${crtfile[4]} ] ;then
printf "\e[47m## %-14s :- %(%a %d %b %y %T)T ##\e[0m\n" ${crtfile[8]} -1
tail -c +$[1+${files[${crtfile[8]}]:-0}] $wpath/${crtfile[8]}
files[${crtfile[8]}]=${crtfile[4]}
fi
done < <( /bin/ls -l $wpath )
sleep 1
done
This will dump each files (with filename not ending by .gz) in /var/log, and watch for modification or new files, then dump new lines.
Demo:
In a first terminal console, hit:
ext=(php css other)
( while :; do
subname=''
((RANDOM%10)) || printf -v subname -- "-%04x" $RANDOM
name=test$subname.${ext[RANDOM%3]}
printf "%-16s" $name
{
date +"%a %d %b %y %T" | tee /dev/fd/5
fortune /usr/share/games/fortunes/bofh-excuses
} >> /tmp/$name
sleep 1
done ) 5>&1
You need to have fortune installed with BOFH excuses librarie.
If you really not have fortune, you could use this instead:
LANG=C ext=(php css other)
( while :; do
subname=''
((RANDOM%10)) || printf -v subname -- "-%04x" $RANDOM
name=test$subname.${ext[RANDOM%3]}
printf "%-16s" $name
{
date +"%a %d %b %y %T" | tee /dev/fd/5
for ((1; RANDOM%5; 1))
do
printf -v str %$[RANDOM&12]s
str=${str// /blah, }
echo ${str%, }.
done
} >> /tmp/$name
sleep 1
done ) 5>&1
This may output something like:
test.css Thu 28 Apr 16 12:00:02
test.php Thu 28 Apr 16 12:00:03
test.other Thu 28 Apr 16 12:00:04
test.css Thu 28 Apr 16 12:00:05
test.css Thu 28 Apr 16 12:00:06
test.other Thu 28 Apr 16 12:00:07
test.php Thu 28 Apr 16 12:00:08
test.css Thu 28 Apr 16 12:00:09
test.other Thu 28 Apr 16 12:00:10
test.other Thu 28 Apr 16 12:00:11
test.php Thu 28 Apr 16 12:00:12
test.other Thu 28 Apr 16 12:00:13
In a second terminal console, hit:
declare -A files
wpath=/tmp
while :; do
while read -a crtfile; do
if [ "${crtfile:0:1}" = "-" ] && [ "${crtfile[8]:0:4}" = "test" ] &&
( [ "${crtfile[8]##*.}" = "css" ] || [ "${crtfile[8]##*.}" = "php" ] ) &&
[ "${files[${crtfile[8]}]:-0}" -lt ${crtfile[4]} ]; then
printf "\e[47m## %-14s :- %(%a %d %b %y %T)T ##\e[0m\n" ${crtfile[8]} -1
tail -c +$[1+${files[${crtfile[8]}]:-0}] $wpath/${crtfile[8]}
files[${crtfile[8]}]=${crtfile[4]}
fi
done < <(/bin/ls -l $wpath)
sleep 1
done
This will each seconds
for all entries in watched directory
search for files (first caracter is -),
search for filenames begining by test,
search for filenames ending by css or php,
compare already printed sizes with new file size,
if new size greater,
print out new bytes by using tail -c and
store new already printed size
sleep 1 seconds
this may output something like:
## test.css :- Thu 28 Apr 16 12:00:09 ##
Thu 28 Apr 16 12:00:02
BOFH excuse #216:
What office are you in? Oh, that one. Did you know that your building was built over the universities first nuclear research site? And wow, aren't you the lucky one, your office is right over where the core is buried!
Thu 28 Apr 16 12:00:05
BOFH excuse #145:
Flat tire on station wagon with tapes. ("Never underestimate the bandwidth of a station wagon full of tapes hurling down the highway" Andrew S. Tannenbaum)
Thu 28 Apr 16 12:00:06
BOFH excuse #301:
appears to be a Slow/Narrow SCSI-0 Interface problem
## test.php :- Thu 28 Apr 16 12:00:09 ##
Thu 28 Apr 16 12:00:03
BOFH excuse #36:
dynamic software linking table corrupted
Thu 28 Apr 16 12:00:08
BOFH excuse #367:
Webmasters kidnapped by evil cult.
## test.css :- Thu 28 Apr 16 12:00:10 ##
Thu 28 Apr 16 12:00:09
BOFH excuse #25:
Decreasing electron flux
## test.php :- Thu 28 Apr 16 12:00:13 ##
Thu 28 Apr 16 12:00:12
BOFH excuse #3:
electromagnetic radiation from satellite debris
Nota: If some file are modified more than one time between two checks, all modification will be printed on next check.
Although not really nice, the following gives (and repeats) the last 50 lines of the newest file in the current directory:
while true; do tail -n 50 $(ls -Art | tail -n 1); sleep 5; done
You can refresh every minute using a cronjob:
$crontabe -e
* * * * * /home/script.sh
if you need to refresh in less than a minute you can use the command "sleep" inside your script.

gammu-smsd RunOnReceive script results exit status 2

I want to forward an SMS using gammu-smsd RunOnReceive.
That is the script I want to run (/var/spool/gammu/forward.sh) and it goes perfectly if I run it from a sudoer or using sudo -u gammu -g gammu /var/spool/gammu/forward.sh
#!/bin/bash
SMS_MESSAGES=1
for i in `seq $SMS_MESSAGES`
do
number="SMS_${i}_NUMBER"
text="SMS_${i}_TEXT"
eval "gammu-smsd-inject TEXT my_number_goes_here -text \"${!number}: ${!text}\""
done
And here is the problem I am experiencing:
Thu 2015/01/29 23:08:57 gammu-smsd[2549]: Starting run on receive: /var/spool/gammu/forward.sh IN20150130_000850_00_+37368214400_00.txt
Thu 2015/01/29 23:08:57 gammu-smsd[2154]: Process failed with exit status 2
Output of ls -l /etc/gammu-smsdrc /var/spool/gammu/ /usr/bin/gammu-smsd*:
-rw-r--r-- 1 root root 457 Jan 29 22:44 /etc/gammu-smsdrc
-rwxrwxrwx 1 root root 14336 Jun 10 2012 /usr/bin/gammu-smsd
-rwxrwxrwx 1 root root 51164 Jun 10 2012 /usr/bin/gammu-smsd-inject
-rwxrwxrwx 1 root root 9972 Jun 10 2012 /usr/bin/gammu-smsd-monitor
/var/spool/gammu/:
total 24
drwxrwxrwx 2 gammu gammu 4096 Jan 28 16:02 error
-rwxrwxrwx 1 gammu gammu 189 Jan 29 22:13 forward.sh
drwxrwxrwx 2 gammu gammu 4096 Jan 29 23:08 inbox
-rw-rw-r-- 1 gammu gammu 3702 Jan 29 23:08 log
drwxrwxrwx 2 gammu gammu 4096 Jan 29 23:07 outbox
drwxrwxrwx 2 gammu gammu 4096 Jan 29 23:07 sent
What happens if I just do ./forward.sh (not root) - so all is OK:
gammu-smsd-inject[2606]: Created outbox message OUTC20150029_231213_00_my_number_here_sms0.txt
Written message with ID /var/spool/gammu/outbox/OUTC20150029_231213_00_my_number_here_sms0.txt
Here is my /etc/gammu-smsdrc
# Configuration file for Gammu SMS Daemon
[gammu]
port = /dev/ttyUSB0
connection = at
[smsd]
service = files
logfile = /var/spool/gammu/log
debuglevel = 2
commtimeout = 1
sendtimeout = 15
statusfrequency = 0
outboxformat = unicode
transmitformat = unicode
RunOnReceive = /var/spool/gammu/forward.sh
inboxpath = /var/spool/gammu/inbox/
outboxpath = /var/spool/gammu/outbox/
sentsmspath = /var/spool/gammu/sent/
errorsmspath = /var/spool/gammu/error/
ps -fe | grep gammu:
gammu 2154 1 0 23:05 ? 00:00:02 /usr/bin/gammu-smsd --daemon --user gammu --pid /var/run/gammu-smsd.pid
cubie 2644 2403 0 23:20 pts/0 00:00:00 grep gammu
Please, help
I had the same problem and I solved it this way:
First add gammu user to sudoers, with no password:
type: $ sudo visudo
and add: gammu ALL=(ALL) NOPASSWD: ALL
Then run gammu-smsd as root user:
in /etc/init.d/gammu-smsd
change USER=gammu to USER=root
save it and don't forget to restart daemon: service gammu-smsd restart
In RunOnReceive script add sudo in front of gammu-smsd-inject:
e.g.: sudo gammu-smsd-inject TEXT my_tel_num -text "Hello world!"
I hope this will work for you too!
P.S.: I use Gammu version 1.31.90.

procmail disregards /etc/group?

sample procmailrc:
SHELL=/bin/bash
LOGFILE=$HOME/procmail.log
VERBOSE=yes
:0
* ^Subject: envdump please$
{
LOG="`id`"
:0
/dev/null
}
/etc/group file contains (note the other usernames are vain attempts to make this work):
someuser:x:504:
s3:x:505:someuser,someotheruser,postfix,postdrop,mail,root
If I run as "someuser" the command id:
[someuser#lixyz-pqr ~]$ id
uid=504(someuser) gid=504(someuser) groups=504(someuser),505(s3)
However when I run procmail by sending an email with the subject "envdump please", the 505/s3 group disappears (this is in procmail.log):
procmail: [17618] Mon Dec 19 17:39:50 2011
procmail: Match on "^Subject: envdump please$"
procmail: Executing "id"
procmail: Assigning "LOG=uid=504(someuser) gid=504(someuser) groups=504(someuser)"
uid=504(someuser) gid=504(someuser) groups=504(someuser)procmail: Assigning "LASTFOLDER=/dev/null"
this server is running Fedora 14 with Postfix 2.7.5
Procmail wasn't installed setuid.
for background, it should look like:
[root#li321-238 postfix]# ls -l /usr/bin/procmail
-rwsr-sr-x. 1 root mail 92816 Jul 28 2009 /usr/bin/procmail
which you can set up via:
chmod ug+s /usr/bin/procmail

Resources