Per the documentation, when you create a detached session (new-session -d
), it defaults to a size of 80×24. If you attach with a terminal window that is actually 24 lines high (or 25, since tmux uses one for a status line), then you should find that the below-Vim pane does in fact end up with just five lines.
The problem comes when you attach to the session with a terminal window that is much taller than 24 lines. When you do this, tmux resizes the panes to fill the full terminal window. The lower pane grows past its original five lines when this happens.
One way to work around this problem is to create the detached session with an initial size that matches that of the terminal window from which you will eventually attach to the session. One semi-portable way to do this is to parse the output of stty size
(some shells also provide LINES and COLUMNS parameters (especially when in interactive mode), but these parameters are not always available and reliable in shell scripts).
set -- $(stty size) # $1 = rows $2 = columns
tmux -2 new-session -d -s "$SESSION" -x "$2" -y "$(($1 - 1))" # status line uses a row
The failed to connect to server: Connection refused
message comes from your tmux has-session
command. It is reporting that it there is no existing server. Since you are only interested in the exit code, you can probably just send the output to /dev/null
to avoid seeing it at all. You can also put the command directly into the if
statement:
if tmux has-session -t "$SESSION" 2>/dev/null; then
⋮
fi
Incidentally, you should almost always put your parameter expansions in double quotes (to avoid word splitting and glob expansion). You only have the one parameter and its value (copied from USER
) is (usually) probably safe not to quote, but it is a good habit to always quote your expansions in almost all contexts.
Best Answer
notify-send
uses D-Bus.Normally when you're logged in to your desktop and use
notify-send
, the variableDBUS_SESSION_BUS_ADDRESS
is in your environment and points to the proper socket (e.g. in my Kubuntu the value of the variable isunix:path=/run/user/1000/bus
where1000
is my UID). The socket is created when you first log in.For
notify-send
to work as expected, the following conditions must be met:DBUS_SESSION_BUS_ADDRESS
environment variable. It seems if the variable is not set then the tool will try$XDG_RUNTIME_DIR/bus
.Your script, when it was invoked from
/etc/init.d/
, run as root. I think there was no variable, no socket and no notifying program for root.When the script was invoked from your personal crontab, it run under your user but the variables were not set.
One method to fix this is to add these lines to your script before
notify-send
:(General note: use
env | grep ^DBUS_SESSION_BUS_ADDRESS=
to verify if your OS uses this pattern).Then you want to run the script as your user and to be logged in when
notify-send
runs (so the socket exists) and to be logged in to your desktop (so your desktop environment receives and displays the notification). Solenotify-send
(like in your example) will most certainly run before you log in; the message from it will be discarded.But if the script does some real job that takes time, then
notify-send
at the end will make sense. Even in this case you may log in too late to receive the notification though.A better way to know if your script has finished is to log to a file:
This will not work well if more than one instance runs at a time; but you run just one instance per reboot, so it should be fine. Use
notify-send
as a secondary, less reliable yet convenient channel. Regardless of how late you log in, you can always examine the file and know whether you have already missed the notification or it is yet to come.