Set Permanently ulimit -n / open files in ubuntu

The currently configured system max can be seen with the command

cat /proc/sys/fs/file-max

This value is the total for the system, not per process.

# available limit
user@ubuntu:~$ ulimit -n
1024

# To increase the available limit to say 65535
user@ubuntu:~$ sudo vim /etc/sysctl.conf

# add the following line to it
fs.file-max = 65535

# run this to refresh with new config
user@ubuntu:~$ sudo sysctl -p
# edit the following file
user@ubuntu:~$ sudo vim /etc/security/limits.conf

# add following lines to it
* soft     nproc          65535    
* hard     nproc          65535   
* soft     nofile         65535   
* hard     nofile         65535
root soft     nproc          65535    
root hard     nproc          65535   
root soft     nofile         65535   
root hard     nofile         65535

# edit the following file
user@ubuntu:~$ sudo vim /etc/pam.d/common-session
# fedora => /etc/pam.d/login

# add this line to it
session required pam_limits.so

# logout and login and try the following command
user@ubuntu:~$ ulimit -n
65535

References
https://medium.com/@muhammadtriwibowo/set-permanently-ulimit-n-open-files-in-ubuntu-4d61064429a
https://askubuntu.com/questions/1110544/what-is-the-maximum-recommended-number-of-open-file-descriptors-on-my-vmware-whi

Install Shadowsocks with v2ray-plugin

sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y && sudo apt-get autoremove -y && sudo apt-get clean && sudo apt-get install build-essential haveged -y
## Ubuntu 18.04/16.04 or Debian 9
wget -O ubuntu-ss-install.sh https://github.com/M3chD09/shadowsocks-with-v2ray-plugin-install/raw/master/ubuntu-ss-install.sh
chmod +x ubuntu-ss-install.sh
./ubuntu-ss-install.sh
# Manage shadowsocks with systemctl
systemctl status shadowsocks
systemctl start shadowsocks
systemctl stop shadowsocks
nano /etc/shadowsocks-libev/config.json
{
    "server":"0.0.0.0",
    "server_port":80,
    "password":"PASSWORD",
    "timeout":300,
    "user":"nobody",
    "method":"aes-256-gcm",
    "nameserver": "8.8.8.8",
    "fast_open":true,
    "reuse_port":true,
    "no_delay":true,
    "plugin":"/etc/shadowsocks-libev/",
    "plugin_opts":"server"
}

References
https://github.com/M3chD09/shadowsocks-with-v2ray-plugin-install
https://gist.github.com/rampageX/134fa08a547d4b1eabd754c279b12676

Run a Tor Bridge on Ubuntu

RunAsDaemon 1
BridgeRelay 1
ORPort 16001
ServerTransportPlugin obfs4 exec /usr/bin/obfs4proxy
ServerTransportListenAddr obfs4 0.0.0.0:16002
ExtORPort auto
ContactInfo [email protected]
Nickname yourNickname
PublishServerDescriptor 0

References
https://community.torproject.org/relay/setup/bridge/debian-ubuntu/
https://scottlinux.com/2016/01/16/how-to-run-a-tor-bridge-on-linux/
https://tails.boum.org/doc/first_steps/startup_options/bridge_mode/index.en.html

Download apk file using Download Manager on Android

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
//get destination to update file and set Uri
   //TODO: First I wanted to store my update .apk file on internal storage for my app but apparently android does not allow you to open and install
   //aplication with existing package from there. So for me, alternative solution is Download directory in external storage. If there is better
   //solution, please inform us in comment
   String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
   String fileName = "AppName.apk";
   destination += fileName;
   final Uri uri = Uri.parse("file://" + destination);

   //Delete update file if exists
   File file = new File(destination);
   if (file.exists())
   //file.delete() - test this, I think sometimes it doesnt work
       file.delete();

   //get url of app on server
   String url = Main.this.getString(R.string.update_app_url);

   //set downloadmanager
   DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
   request.setDescription(Main.this.getString(R.string.notification_description));
   request.setTitle(Main.this.getString(R.string.app_name));

   //set destination
   request.setDestinationUri(uri);

   // get download service and enqueue file
   final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
   final long downloadId = manager.enqueue(request);

   //set BroadcastReceiver to install app when .apk is downloaded
   BroadcastReceiver onComplete = new BroadcastReceiver() {
       public void onReceive(Context ctxt, Intent intent) {
           Intent install = new Intent(Intent.ACTION_VIEW);
           install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
           install.setDataAndType(uri,
                   manager.getMimeTypeForDownloadedFile(downloadId));
           startActivity(install);

           unregisterReceiver(this);
           finish();
       }
   };
   //register receiver for when .apk download is compete
   registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

References
https://stackoverflow.com/questions/4967669/android-install-apk-programmatically
https://stackoverflow.com/questions/39147608/android-install-apk-with-intent-view-action-not-working-with-file-provider/40131196#40131196

Execute a command whenever a file changes

You can do this with inotifywait utility from inotify-tools package

while true ; do
  inotifywait -e delete_self "/tmp/fileToMonitor.txt" \
    && cp new_file "/tmp/fileToMonitor.txt"
done
[ "$UID" -eq 0 ] || exec sudo "$0" "$@"

while true ; do
  inotifywait -e modify "/etc/resolv.conf" && ./dns.sh
done

References
https://superuser.com/questions/939600/how-to-get-notified-when-a-specific-file-is-deleted-in-linux
https://superuser.com/questions/181517/how-to-execute-a-command-whenever-a-file-changes
http://man7.org/linux/man-pages/man1/inotifywait.1.html#EVENTS