How to close firefox at a given time in linux? - linux

The challenge is to be able to close firefox at a given time, to protect a person from its own "internet addiction" and ensure some rest during the night.
In this case, my partner asked to shut firefox down at 22h00, as she was staying up at night and then the next day being tired.
And when she happened to want to open firefox after 22h00, close it automatically after 15 minutes from when she opened it.
EDIT
I've written this question with an answer in place already where I created a shell script and then integrated with linux file.
There in an error on the code thatif time is before 22h00, it still is adding 15 minutes.
now=$(date +'%R')
KILL_DATE=$(date -d "22:00 today" +'%R')
if [ "$now" > "$KILL_DATE" ]; then
KILL_DATE=$(date -d "$now today + 15 minutes" +'%R')
fi
exec echo "pkill -f firefox" | at $KILL_DATE
exec $MOZ_PROGRAM "$#"

The solution I found for this was to:
replicate firefox file on /usr/bin/ to firefox_daytime
update the lines where firefox is loaded and use the "at" command to execute the background killing task at the specified time.
This was chosen because usually she opens firefox from the shortcuts on her "start menu".
This was only tested using openSuse.
specifically, the code for achieving this was:
now=$(date +'%R')
kill_date=$(date -d "22:00 today" +'%R')
if [ $now -gt $kill_date ]; then
kill_date=$(date -d "$now today + 15 minutes" +'%R')
fi
exec echo "pkill -f firefox" | at $kill_date
exec $MOZ_PROGRAM "$#"
Which can be found on the last lines of the new file.
/usr/bin/firefox_daytime
#!/bin/sh
#
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is mozilla.org Code.
#
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 1998
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Wolfgang Rosenauer <wolfgang.rosenauer#suse.de>
# <wr#rosenauer.org>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
##
## Usage:
##
## $ mozilla [args]
##
## This script is meant to run a mozilla program from the mozilla
## rpm installation.
##
## The script will setup all the environment voodoo needed to make
## mozilla work.
cmdname=`basename $0`
##
## Variables
##
MOZ_DIST_BIN="/usr"
MOZ_DIST_LIB="/usr/lib64/firefox"
MOZ_APPNAME="firefox"
MOZ_PROGRAM="$MOZ_DIST_LIB/$MOZ_APPNAME"
MOZ_APP_LAUNCHER="$MOZ_DIST_LIB/$MOZ_APPNAME.sh"
if [ "$0" = "$MOZ_APP_LAUNCHER" ]; then
[ -h "/usr/bin/$MOZ_APPNAME" ] && \
_link=$(readlink -f "/usr/bin/$MOZ_APPNAME")
if [ "$_link" = "$MOZ_APP_LAUNCHER" ]; then
export MOZ_APP_LAUNCHER="/usr/bin/$MOZ_APPNAME"
fi
else
export MOZ_APP_LAUNCHER="/usr/bin/$MOZ_APPNAME"
fi
mozilla_lib=`file $MOZ_PROGRAM`
LIB=lib
echo $mozilla_lib | grep -q -E 'ELF.64-bit.*(x86-64|S/390|PowerPC)' && LIB=lib64
BROWSER_PLUGIN_DIR=/usr/$LIB/browser-plugins
if [ ! -d $BROWSER_PLUGIN_DIR ]; then
BROWSER_PLUGIN_DIR=/opt/netscape/plugins
fi
MOZILLA_FIVE_HOME="$MOZ_DIST_LIB"
export MOZILLA_FIVE_HOME
LD_LIBRARY_PATH=$MOZ_DIST_LIB${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export LD_LIBRARY_PATH
# needed for SUN Java under Xorg >= 7.2
export LIBXCB_ALLOW_SLOPPY_LOCK=1
##
if [ -z "$MOZ_PLUGIN_PATH" ]; then
export MOZ_PLUGIN_PATH=$BROWSER_PLUGIN_DIR
else
# make sure that BROWSER_PLUGIN_DIR is in MOZ_PLUGIN_PATH
echo "$MOZ_PLUGIN_PATH" | grep "$BROWSER_PLUGIN_DIR" 2>&1 >/dev/null
_retval=$?
if [ ${_retval} -ne 0 ]; then
export MOZ_PLUGIN_PATH=$MOZ_PLUGIN_PATH:$BROWSER_PLUGIN_DIR
fi
fi
# disable Gnome crash dialog (doesn't make sense anyway)
export GNOME_DISABLE_CRASH_DIALOG=1
moz_debug=0
script_args=""
pass_arg_count=0
while [ $# -gt $pass_arg_count ]
do
case "$1" in
-d | --debugger)
moz_debugger=$2;
if [ "${moz_debugger}" != "" ]; then
shift 2
moz_debug=1
else
echo "-d requires an argument"
exit 1
fi
;;
*)
# Move the unrecognized argument to the end of the list.
arg="$1"
shift
set -- "$#" "$arg"
pass_arg_count=`expr $pass_arg_count + 1`
;;
esac
done
if [ $moz_debug -eq 1 ]; then
tmpfile=`mktemp /tmp/mozargs.XXXXXX` || { echo "Cannot create temporary file" >&2; exit 1; }
trap " [ -f \"$tmpfile\" ] && /bin/rm -f -- \"$tmpfile\"" 0 1 2 3 13 15
echo "set args ${1+"$#"}" > $tmpfile
echo "run" >> $tmpfile
echo "$moz_debugger $MOZ_PROGRAM -x $tmpfile"
exec $moz_debugger "$MOZ_PROGRAM" -x $tmpfile
else
now=$(date +'%R')
kill_date=$(date -d "22:00 today" +'%R')
if [ $now -gt $kill_date ]; then
kill_date=$(date -d "$now today + 15 minutes" +'%R')
fi
exec echo "pkill -f firefox" | at $kill_date
exec $MOZ_PROGRAM "$#"
fi

Related

Having an issue wit git bash on Window 10 a recent change has stopped me from being able to update up it

I have the Git version 2.31.1.windows.1 installed and have tied re-installing it. I made a change to it to add a new alias. One I did I tried to source the file to reload it and received this error.
bash: /c/Users/DawsonSchaffer/.bashrc: Bad address
So I restored my previous version, which was working fine. However I still get the same error now. I know this isn't a lot of here is my .bashrc file
# History settings
HISTCONTROL=ignoreboth
shopt -s histappend
HISTSIZE=1000
HISTFILESIZE=10000
HISTTIMEFORMAT="%F %T "
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
# load color variables
if [ -f ~/.bash_colors ]; then
. ~/.bash_colors
fi
# prompt
if [ "$color_prompt" = yes ]; then
PS1="${GREEN}ganymede#${BLUE}dawson:${ICYAN}\W${RESET}${IYELLOW} \$(parse_git_branch) $ "
else
PS1='${debian_chroot:+($debian_chroot)}\u#\h:\w\$ '
fi
unset color_prompt force_color_prompt
# get current branch in git repo
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
# function parse_git_branch() {
# BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
# if [ ! "${BRANCH}" == "" ]
# then
# STAT=`parse_git_dirty`
# echo "[${BRANCH}${STAT}]"
# else
# echo ""
# fi
# }
# file colors
LS_ORIGINAL=LS_COLORS
LS_COLORS='di=93:*.py=92:*.qml=92sb'
export LS_COLORS
# Alias definitions.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# Alias functions.
if [ -f ~/.bash_functions ]; then
. ~/.bash_functions
fi
# virtual environment home
export WORKON_HOME='C:\Users\DawsonSchaffer\Documents\ProjectsDirectory\virtualenvirinments'
Any help would greatly appreciated.
Thanks
Dawson
My apologies, after scanning the files several times I finally found that I had a funky character in my .bashrc once I removed it every is working perfectly.
Thanks
Dawson

Start an Electron app at boot on Raspberry Pi 3 with yocto

I built an image with X11 using yocto for a Raspberry Pi 3 and a touchscreen. I can start my app built with Electron (chromium) by running commands manually in a serial session:
export DISPLAY=:0
/usr/lib/node/electron/dist/electron --no-sandbox /home/root/app
I though to use an init.d script to do it automatically at startup but I'd like to do it the proper way. I tried to create an .Xsession file in my user directory with the commands above but it doesn't work and I don't know if I can get logs of what happened.
According to this wiki, there is a lot of steps at X11 startup. Currently, I only see a Terminal (from Matchbox I guess) and a mouse cursor.
What's the "standard" way to start an app with the system and is there a way to remove the cursor for a touchscreen?
Edit
Here is the content of my /etc/X11 directory:
Xsession
Xsession.d/
xinit/
xorg.conf
Xsession:
#!/bin/sh
if [ -x /usr/bin/dbus-launch ]; then
# As this is the X session script, always start a new DBus session.
eval `dbus-launch --sh-syntax --exit-with-session </dev/null`
echo "D-BUS per-session daemon address is: $DBUS_SESSION_BUS_ADDRESS"
fi
. /etc/profile
if [ -f $HOME/.profile ]; then
. $HOME/.profile
fi
SYSSESSIONDIR=/etc/X11/Xsession.d
export CLUTTER_DISABLE_MIPMAPPED_TEXT=1
for SESSIONFILE in $SYSSESSIONDIR/*; do
set +e
case "$SESSIONFILE" in
*.sh)
. "$SESSIONFILE"
;;
*.shbg)
"$SESSIONFILE" &
;;
*~)
# Ignore backup files
;;
*)
"$SESSIONFILE"
;;
esac
set -e
done
exit 0
xorg.conf: empty.
Xsession.d/:
13xdgbasedirs.sh
30xinput_calibrate.sh
89xdgautostart.sh
90XWindowManager.sh
89xdgautostart.sh:
XDGAUTOSTART=/etc/xdg/autostart
if [ -d $XDGAUTOSTART ]; then
for SCRIPT in $XDGAUTOSTART/*; do
CMD=`grep ^Exec= $SCRIPT | cut -d '=' -f 2`
$CMD &
done
fi
90XWindowManager.sh:
if [ -x $HOME/.Xsession ]; then
exec $HOME/.Xsession
elif [ -x /usr/bin/x-session-manager ]; then
exec /usr/bin/x-session-manager
else
exec /usr/bin/x-window-manager
fi
There is also a file /etc/xserver-nodm/Xserver:
#!/bin/sh
# This script is only needed to make sure /etc/X11/xserver-common
# can affect XSERVER, ARGS & DPI: otherwise systemd could just use
# /etc/default/xserver-nodm as EnvironmentFile and sysvinit could just
# source the same file
. /etc/profile
# load default values for XSERVER, ARGS, DISPLAY...
. /etc/default/xserver-nodm
# Allow xserver-common to override ARGS, XSERVER, DPI
if [ -e /etc/X11/xserver-common ] ; then
. /etc/X11/xserver-common
if [ ! -e $XSERVER ] ; then
XSERVER=$(which $XSERVER)
fi
fi
if [ -n "$DPI" ] ; then
ARGS="$ARGS -dpi $DPI"
fi
exec xinit /etc/X11/Xsession -- $XSERVER $DISPLAY $ARGS $*
and a file /etc/rc5.d/S09xserver-nodm:
#!/bin/sh
#
### BEGIN INIT INFO
# Provides: xserver
# Required-Start: $local_fs $remote_fs dbus
# Required-Stop: $local_fs $remote_fs
# Default-Start: 5
# Default-Stop: 0 1 2 3 6
### END INIT INFO
killproc() { # kill the named process(es)
pid=`/bin/pidof $1`
[ "$pid" != "" ] && kill $pid
}
read CMDLINE < /proc/cmdline
for x in $CMDLINE; do
case $x in
x11=false)
echo "X Server disabled"
exit 0;
;;
esac
done
case "$1" in
start)
. /etc/profile
#default for USER
. /etc/default/xserver-nodm
echo "Starting Xserver"
if [ "$USER" != "root" ]; then
# setting for rootless X
chmod o+w /var/log
chmod g+r /dev/tty[0-3]
# hidraw device is probably needed
if [ -e /dev/hidraw0 ]; then
chmod o+rw /dev/hidraw*
fi
fi
# Using su rather than sudo as latest 1.8.1 cause failure [YOCTO #1211]
su -l -c '/etc/xserver-nodm/Xserver &' $USER
# Wait for the desktop to say its finished loading
# before loading the rest of the system
# dbus-wait org.matchbox_project.desktop Loaded
;;
stop)
echo "Stopping XServer"
killproc xinit
sleep 1
chvt 1 &
;;
restart)
$0 stop
$0 start
;;
*)
echo "usage: $0 { start | stop | restart }"
;;
esac
exit 0
The correct way to define a complete X session depends on your session manager: on Yocto that is often matchbox-session or mini-x-session. From your description I'd guess you're using mini-x-session (it happens to start a terminal and a window-manager if session file is not found).
Quoting mini-x-session:
if [ -e $HOME/.mini_x/session ]
then
exec $HOME/.mini_x/session
fi
if [ -e /etc/mini_x/session ]
then
exec /etc/mini_x/session
fi
So adding a /etc/mini_x/session script should work.
By the way, in your session file you may also want to start a window manager (X can do weird things without one):
your-app-here &
exec matchbox-window-manager

Shell script does not exit to the prompt

I have a shell script ws_appl_process that stop/start WebSphere servers:
#!/bin/sh
# ------------------------------------
# WebSphere Application Server process
# ------------------------------------
# PLEASE NOTE: This script assumes no spaces in the server names
# Finally, alter $APPSERVERS if server1 is required to start
PROF_HOME="/opt/WebSphere/AppServer855/profiles/xxxxxx"
## set colors
ec=`tput setaf 1` # error color
rc=`tput sgr 0` # reset color to default
if [ ! -d $PROF_HOME ];then
echo " "
echo "${ec}WARNING:${rc} unable to locate directory $PROF_HOME! No action taken."
echo " "
else
. $PROF_HOME/bin/setupCmdLine.sh
SERVERDIR=$PROF_HOME/config/cells/$WAS_CELL/nodes/$WAS_NODE/servers
APPSERVERS=`ls $SERVERDIR | egrep -v "jmsserver|nodeagent|web$"`
case "$1" in
'start')
# delay start by 30 seconds to let OS 'calm down' on startup
sleep 30
# start jmmserver first, if applicable
if [ -f $SERVERDIR/jmsserver/server.xml ]; then
$PROF_HOME/bin/startServer.sh jmsserver
fi
# start all servers found in config directory
for i in $APPSERVERS;do
$PROF_HOME/bin/startServer.sh $i &
sleep 1
done
;;
'stop')
for i in $APPSERVERS;do
$PROF_HOME/bin/stopServer.sh $i &
done
if [ -f $SERVERDIR/jmsserver/server.xml ]; then
$PROF_HOME/bin/stopServer.sh jmsserver
fi
;;
*)
echo "Usage: $0 { start | stop }"
exit 1
esac
fi
exit $?
Running this script
./ws_appl_process stop
will do what is intended, e.g. stop or start.
However, the script does NOT return to the prompt e.g. user#server:~>.
I have to manually hit the <RETURN> or <ENTER> key to get to the prompt.
I have tried to modify the last command
exit $?
to
exit 0
or
exit $?
exit 0
with no luck.
I need the script to return automatically to the prompt. The reason is that I am automating the monitor of the servers, and eventually bring them back, i.e.
./ws_appl_process start
The OS:
Linux xxxxxx 3.0.101-108.101-default #1 SMP Tue Aug 13 08:31:17 UTC 2019 (d0a7b7a) x86_64 x86_64 x86_64 GNU/Linux
The shell:
GNU bash, version 3.2.57(2)-release (x86_64-suse-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
How can I force the script back to the prompt?
Appreciate any comments.

SSH through Putty comes up with a "no such file or directory"

I'm trying to access out Linux server through Putty but for some reason after a somewhat successful login it just throws a "/bin/bash no such file or directory" then Putty closes.
Contents of bashrc:
# /etc/bashrc
# System wide functions and aliases
# Environment stuff goes in /etc/profile
# By default, we want this to get set.
# Even for non-interactive, non-login shells.
if [ $UID -gt 99 ] && [ "`id -gn`" = "`id -un`" ]; then
umask 002
else
umask 022
fi
# are we an interactive shell?
if [ "$PS1" ]; then
case $TERM in
xterm*)
if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
else
PROMPT_COMMAND='echo -ne "\033]0;${USER}#${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
fi
;;
screen)
if [ -e /etc/sysconfig/bash-prompt-screen ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
else
PROMPT_COMMAND='echo -ne "\033_${USER}#${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\033\\"'
fi
;;
*)
[ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
;;
esac
# Turn on checkwinsize
shopt -s checkwinsize
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u#\h \W]\\$ "
fi
if ! shopt -q login_shell ; then # We're not a login shell
# Need to redefine pathmunge, it get's undefined at the end of /etc/profile
pathmunge () {
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}
# Only display echos from profile.d scripts if we are no login shell
# and interactive - otherwise just process them to set envvars
for i in /etc/profile.d/*.sh; do
if [ -r "$i" ]; then
if [ "$PS1" ]; then
. $i
else
. $i >/dev/null 2>&1
fi
fi
done
unset i
unset pathmunge
fi
# vim:ts=4:sw=4
What could be causing this?
Thanks!
Well bash is more than likely located at a different location on this linux box to /bin/bash
I've found this to be the case on different boxes and I've changed my .profile script in my home directory to not directly execute bash, could be a solution in your case. So when you log in you stay in the bourne shell, and then go into the bash shell only if you explicitly type bash.
Check your /etc/ssh/sshd_config to make sure that you don't have a chroot directory set. If you do, you will need to either create a bin directory in the chroot directory and either copy or link the necessary binaries into that directory.
Or you could always comment that line out in the config.
Either way, restart sshd and test.

delete backups older than 7 days from remote ftp using ncftp

I`m currently using this script from cyberciti
#!/bin/sh
# System + MySQL backup script
# Full backup day - Sun (rest of the day do incremental backup)
# Copyright (c) 2005-2006 nixCraft <http://www.cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# Automatically generated by http://bash.cyberciti.biz/backup/wizard-ftp-script.php
# ---------------------------------------------------------------------
### System Setup ###
DIRS="/home /etc /var/www"
BACKUP=/tmp/backup.$$
NOW=$(date +"%d-%m-%Y")
INCFILE="/root/tar-inc-backup.dat"
DAY=$(date +"%a")
FULLBACKUP="Sun"
### MySQL Setup ###
MUSER="admin"
MPASS="mysqladminpassword"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
### FTP server Setup ###
FTPD="/home/vivek/incremental"
FTPU="vivek"
FTPP="ftppassword"
FTPS="208.111.11.2"
NCFTP="$(which ncftpput)"
### Other stuff ###
EMAILID="admin#theos.in"
### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :
### See if we want to make a full backup ###
if [ "$DAY" == "$FULLBACKUP" ]; then
FTPD="/home/vivek/full"
FILE="fs-full-$NOW.tar.gz"
tar -zcvf $BACKUP/$FILE $DIRS
else
i=$(date +"%Hh%Mm%Ss")
FILE="fs-i-$NOW-$i.tar.gz"
tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS
fi
### Start MySQL Backup ###
# Get all databases name
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done
### Dump backup using FTP ###
#Start FTP backup using ncftp
ncftp -u"$FTPU" -p"$FTPP" $FTPS<<EOF
mkdir $FTPD
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput *
quit
EOF
### Find out if ftp backup failed or not ###
if [ "$?" == "0" ]; then
rm -f $BACKUP/*
else
T=/tmp/backup.fail
echo "Date: $(date)">$T
echo "Hostname: $(hostname)" >>$T
echo "Backup failed" >>$T
mail -s "BACKUP FAILED" "$EMAILID" <$T
rm -f $T
fi
It works nice, but my backups take up too much space on remote server, so I would like to modify this script so the ones that are older that 7 days are deleted.
Can someone tell me what to edit? I have no knowledge of shell scripting or ncftp commands though.
I don't have a practical method of trying what I type below - I would suspect that this works, but if not it will at least show the right idea. Please don't use these mods without thorough testing first - if I have stuffed up it could delete your data, but here goes:
Underneath the line:NOW=$(date +"%d-%m-%Y")
add:
DELDATE=$(date -d "-7 days" +"%d-%m-%Y")
after the line: ncftp -u"$FTPU" -p"$FTPP" $FTPS<<EOF
add:
cd $FTPD/$DELDATE
rm *
cd $FTPD
rmdir $DELDATE
The theory behind these changes is as follows:
The first addition calculates what the date was 7 days ago.
The second addition attempts to delete the old information.

Resources