CRON Cheatsheet

I’m tired of always looking up CRON scheduling, so here it is including some examples.

CRON Scheduling table:

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of week (0 - 6)
# │ │ │ │ │                                      
# │ │ │ │ │
# │ │ │ │ │
# * * * * *  command to execute

Note that the day of week (0-6) typically starts with Sunday as the first index (0), so Monday = 1, Tuesday = 2 and so on. On some systems you might be able to also use 7 as Sunday.

CRON Examples:

Note that all the cron jobs output are redirected to /tmp/log.txt. If omitted, the default is to send an email to the sysadmin email address (if configured).

Run Python script every night at 02 am

* 02 * * * python /path/to/script.py >> /tmp/log.txt 2>&1

Run PHP script once every two hours

0 */2 * * * php /path/to/script.php >> /tmp/log.txt 2>&1

Run shell script every 10 minutes

*/10 * * * * /path/to/script.sh >> /tmp/log.txt 2>&1

Workaround for running a script every 15 seconds

* * * * * /path/to/script.sh >> /tmp/log.txt 2>&1

* * * * * sleep 15; /path/to/script.sh >> /tmp/log.txt 2>&1

* * * * * sleep 30; /path/to/script.sh >> /tmp/log.txt 2>&1

* * * * * sleep 45; /path/to/script.sh >> /tmp/log.txt 2>&1

Run a script every weekday at midnight

0 0 * * 1-5 /path/to/script.sh >> /tmp/log.txt 2>&1

Run script every Monday in June, July and August at 0530

30 05 * 6-8 1 /path/to/script.sh >> /tmp/log.txt 2>&1

Related post:

Linux Cheatsheet

Powershell Snippets

List local user accounts

Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'"


List local user accounts and parse array

$local_users = Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | findstr "Caption";
for ($i = 0; $i -lt $local_users.Count; $i++) {
    Write-Host $local_users[$i].split("\\")[1];
}


List all installed Windows updates

Get-Hotfix-computername <hostname>Select HotfixID, InstalledOn |Sort-Object InstalledOn
Output will look something like:

tail equivalent in Powershell

Get-Content filename.txt -Tail 30

List all AD groups for user

Save the following code to a script called getgroups.ps1:

Param(   
    [Parameter(Mandatory=$false)][string]$username 
) 
(New-Object System.DirectoryServices.DirectorySearcher(
    "(&(objectCategory=User)(samAccountName=$($username)))"
 )).FindOne().GetDirectoryEntry().memberOf

Run the script from powershell and enter username as parameter, like this:

.\getgroups.ps1 johndoe

Linux Cheatsheet

Print lines of code (amount) for all .php files in current directory

find . -name '*.php' | xargs wc -l


Create an alias for bash commands and/or custom functions

Add the alias or function to your bash profile config:
nano ~/.bashrc

Example: Add a shorthand command for ls-lah to simply l:

alias l="ls -lah"

Example: Add a custom function for searching for a string in all files in current directory:

grepfor() {
    grep -rnw './' -e "$1"
}

When done, reload your config file by running:
. ~/.bashrc


Search for string in files

grep -rnw '/path/to/dir/' -e 'stringpattern'


List only files with size above 100 MB

find . -type f -size +100M -exec ls -lh {} \;

This command scans the current dir for files above 100 MB and then executes the ls -lh command for those files. You could also say -size -10M to do the same for files below 10 MB in size.


Clean/format USB drives

sudo fdisk -l
sudo mkdosfs /dev/xxx # (i.e sdb1)
sudo mkdir /media/usb
sudo mount /dev/xxx /media/usb


See uptime and average load

uptime


Change timezone

sudo rm /etc/localtime
sudo ln -s /usr/share/zoneinfo/Europe/Oslo /etc/localtime

Replace Europe/Oslo with your preferred timezone


Remove password for local user (make it passwordless)

sudo passwd --delete username


See current users logged on server

users (usernames only)

or

who(includes extra information like logon time and IP address etc)


Show IP addresses for all local network cards (including local loopback)

ifconfig | grep inet | cut -d: -f2 | cut -d" " -f1

or

ifconfig | grep 'inet addr' | cut -d: -f2 | awk '{print $1}'

If you wish to see the IP address for a specific NIC (network interface card), simply append the name after ifconfig, like ifconfig eth0 | ...


Map network share

$ sudo mkdir /mnt/ShareName
$ sudo mount -t cifs -o username=domain\username,password=pw //server/share /mnt/ShareName


List automated ssh attacks to your server from syslog:

$ sudo cat /var/log/* | grep BREAK-IN


This post will be continually updated with useful commands and other notes for Linux environments

Related posts:

MS Cheatsheet

CRON Cheatsheet

Powershell Snippets

MS Cheatsheet

List TCP/IP interfaces on remote computer

netsh -r "hostname" interface ipv4 show interfaces


Get Windows OEM license key

wmic path SoftwareLicensingService get OA3xOriginalProductKey


List environment variables

set

Filter output

set prog


Install .NET Framework 3.5 on Windows 10

Online version:
DISM.EXE /Online /Add-Capability /CapabilityName:NetFx3~~~~

Offline version (requires .cab installer for the .NET framework):
DISM.EXE /Online /Add-Package /PackagePath:C:\Temp\microsoft-windows-netfx3-ondemand-package.cab


Kill a non responding task from command prompt

taskkill /F /IM taskname.exe

/F = Force kill (ignores any prompts etc)

/IM = Image Name (name of process)


Generate WLAN report (run as admin)

netsh wlan show wlanreport

» See an example report here


List all server shares on local domain network

net view /all /domain:company.com


Message (aka net send)

msg /server:hostname username

Example:

msg /server:pc01 johndoe


List all local MAC addresses

getmac /v


Map network share

net use x: \\server\share /user:domain\username password


Remote sysinfo

msinfo32 /computer hostname


See uptime (and more)

net stats srv

if not available, use

net stats workstation


Show Domain Controllers in domain

nltest /dclist:example.com


Set Environment variable on remote computer

setx /s hostname /u domain\user /p pw variable value

Example:

setx /s PC1234 /u itdb\kek /p 123456 JAVA_HOME "C:\Program Files\Java\jre1.8.0_73"


See Windows license expiration information

slmgr /xpr

Alternatively run slmgr /dli for detailed information about your Windows license

For remote computers: slmgr hostname username password /dli


Useful Windows utilities which you can start from the RUN prompt

  • lusrmgr.msc (Local Users Management)
  • sysdm.cpl (System Properties)
  • appwiz.cpl (Programs & Features)
  • eventvwr (Event Viewer)
  • compmgmt.msc (Computer Management)
  • printmanagement.msc (Print Management)
  • devmgmt.msc (Device Management)
  • services.msc (Services)
  • taskschd.msc (Task Scheduler)

 

This post will be continually updated with useful tips for remote troubleshooting and various tools for Microsoft environments

 

Related posts:

CRON Cheatsheet

Powershell Snippets

Linux Cheatsheet