forked from Nephyrin/NephScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliases.sh
141 lines (124 loc) · 5.14 KB
/
aliases.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
# WARNING: Shared aliases between bash/zsh, so needs to be compatible.
# Throws every tunable at the job to beg it to compete for no resources.
#
# systemd-run is used to put it in a scope (and thus cgroup) with the cpu/io weight set to minimum. The cgroup settings
# only have an effect if you have cgroup control delegated to your user slice in systemd and are using cgroups v2,
# however.
#
# nice is an older/simpler way to set CPU weight, which I believe is just factored in to the cgroup's weight in the
# CFS/cgroups world. Including it is a nice fallback if cgroups aren't setup per above. It also causes some system
# monitors to highlight the task as low priority, like htop, that don't currently know about cgroups.
#
# Finally, we set the scheduling priority of the task to 'idle'. This kind of makes its weight a moot point.
#
# If kernel compiles still caused dropped frames running under this, I'm starting a crusade.
lowprio() {
cmd systemd-run --user --scope \
--property=CPUWeight=1 \
--property=IOWeight=1 \
nice -n20 chrt -i 0 "$@"
}
# Run a command detached from the shell, with no connection to the tty or return code, etc..
# Can return failure if the given arguments don't map to a runnable command, and will still print the relevant error.
#
# Commands that parse to a valid file/function/etc, but cannot be launched -- e.g. due to permissions errors -- will
# still silently succeed. This is a limitation of bash/zsh as far as I can tell: that class of failure is simply the
# same as the application failing early.
#
# Ex: A `chmod ugo=x` shell script will be parsed as valid by `command -v`, but hit a permissions error prior to bash
# creating a child process to run it. From the shell's perspective, that command simply failed quickly, and any
# complaining the shell itself writes to stderr is part of the command's output.
#
# Update: The s() alias below is a better way to do this in a systemd system.
x() {
# Warn if you're accidentally launching something in a cgroup (since that is not detached) using the neph cgroup
# functions.
[[ -z $NEPH_CGROUP ]] || ewarn "WARNING: In cgroup"
# If `command -v` doesn't think this parses as something runnable, just run it bare so the shell-level
# error/error-code occurs. This means that `x some_typo --args` doesn't silently succeed, but errors exactly as
# `some_typo --args` would.
if command -v "${1-}" &>/dev/null; then
( "$@" &>/dev/null & disown || true ) # disown could fail if the job didn't start for a reason other than it not
# being a valid command. I'm not sure if there's a way in bash/zsh to say
# "show me all the errors up to opening/exec'ing the command"
else
"$@"
fi
}
# spawns a command with systemd-run, like x() but good for spawning something in the desktop session with proper
# cgrouping and such like top-level apps enjoy.
s()
{
# --user - as this user, not system-level command
# --same-dir - keep this working directory
# --collect - don't leave failed units around for inspection
cmd systemd-run --user --same-dir --collect -- "$@"
}
# Re-execute or switch shell (to load new configs or whatever clearly)
#
# if $SHELL is unset or missing you could `exec /proc/self/exe`, but that's weird enough that an alias shouldn't just do
# it (consider if $SHELL is a wrapper)
reload() { cmd exec -- "$SHELL"; }
rezsh() { cmd exec zsh; }
rebash() { cmd exec bash; }
pic() { s gwenview "$@"; }
gdiff() { cmd git diff --no-index "$@"; }
pd() { git diff --no-index --color=always "$@" | diff-so-fancy; }
rv() { cmd rsync -avy --progress "$@"; }
rvp() { cmd rsync -avy --partial --inplace --progress "$@"; }
iswine()
{
local winepids=($(egrep -lzEi '^WINELOADERNOEXEC=' /proc/*/environ 2>/dev/null \
| sed -r 's/^\/proc\/([0-9]+)\/environ$/\1/'))
[[ ${#winepids[@]} -gt 0 ]] && cmd ps -fp "${winepids[@]}" || ewarn No wine processes found
}
ct()
{
dir=$(mktemp -d -t nephtmp.XXXXXXX)
export NEPH_TEMP_DIR="$dir"
cd "$dir"
}
clt()
{
# (This matches how mktemp picks)
local tmpdir=${TMPDIR:-/tmp}
for x in "$tmpdir"/nephtmp.*; do
[[ -d $x ]] || continue
local pids
pids=($(lsof -Fp +D "$x" | tr -d p))
if [[ -z $pids ]]; then
estat "Removing $x"
cmd rm -rf --one-file-system -- "$x"
# If this is our cached directory nuke it
[[ $x != ${NEPH_TEMP_DIR-} ]] || unset NEPH_TEMP_DIR
else
estat "Skipping $x, in use:"
ps -p "${pids[@]}" | einfo_pipe
fi
done
}
rt()
{
if [[ -n $NEPH_TEMP_DIR && -d $NEPH_TEMP_DIR ]]; then
cd -- "$NEPH_TEMP_DIR"
else
unset NEPH_TEMP_DIR
estat "No temp dir in context"
fi
}
service() {
if which systemctl &>/dev/null; then
cmd sudo systemctl -- "$2" "$1"
elif [[ -d /etc/rc.d ]]; then
cmd sudo /etc/rc.d/"$1" "$2";
else
eerr "Don't know how to modify services on this system"
return 1
fi
}
lx() { ls++ --potsf "$@"; }
lxx() { ls++ --potsf -tr "$@"; }
v() { cmd youtube-dl "$@"; }
# Include local aliases
[[ ! -f $HOME/.aliases.local.sh ]] || source "$HOME"/.aliases.local.sh