SSL Script Development Guide

Want to send certificates somewhere the panel has no built-in script for (your own server, FTP, a shared folder), or validate automatically with a DNS provider the panel doesn't support? A small script does it. This guide first walks you from an empty file to a first script running in the panel, then explains the header, what a script receives, passing passwords safely and what to do when it fails, and ends with complete examples you can adapt. Every step has a screenshot or a command to copy, and every command is explained line by line.

Cover of the SSL certificate and script automation video Video: certificates and script automationYouTube · 7:35 · opens in a new tab
Who this is for

Anyone who can log in to Linux over SSH and type a few commands. You don't need to know scripting: the first part starts from an empty file and walks you through.

What you need
  • A server with Nodekeeper installed that you can log in to as root over SSH (scripts live on that server)
  • At least one issued certificate in the panel; if you have none, follow the free SSL certificate guide first
  • A text editor you can use (nano is the simplest), or just copy the commands here
What you will have
  • A script of your own in the panel's dropdown
  • It runs after every issue and renewal, with the result in the certificate details
  • The know-how to write useful scripts the same way: certificate uploads, automatic DNS validation

First: how a script gets used

The Automation Scripts area at the bottom of the certificate form has two slots. Once your script is in place it shows up in the matching dropdown:

  • Issuing script (# type: issuing): runs during validation and makes the CA validation pass, for example by adding a validation record at your DNS provider.
  • Post-issuance script (# type: post): runs after every issue and every renewal and sends the new certificate where it needs to go, for example a cloud provider, another server, or a service reload.

Users pick a script in the form and fill in the fields declared in its header; Nodekeeper calls the script with positional arguments (and environment variables) when it is time and records the result in the certificate details. If you have not used the bundled scripts yet, read Certificate Scripts: Validate and Deploy and Free SSL Certificate Guide first.

Terms explained
Script
A text file with a list of commands. Nodekeeper runs it for you when it is time. All scripts in this guide are Bash scripts (extension .sh).
Issuing script
Runs while a certificate is being requested, during validation, and helps the CA validate, for example by adding a validation record at your DNS provider.
Post-issuance script
Runs after the certificate is issued and after every renewal and sends the new certificate where it is needed, for example another server, then reloads a service.
Header directives
The first lines of the script, comments such as # name: and # type:. The panel reads them to decide the script's name, which dropdown it appears in and which fields the user fills in.
Positional arguments ($1, $2...)
The values Nodekeeper hands to the script, in order. $1 is the first, $5 the fifth. This is how the script learns the domain and where the certificate file is.
Exit code
A number the script returns when it ends. 0 means success; anything else means failure. Nodekeeper uses it to decide whether to retry.
  1. The two script slots

    Add a certificate in Domains → Certificates; the Automation Scripts area is at the bottom of the dialog: the issuing script on the left, the post-issuance script on the right. Every executable script in shell/ssl/ appears in one of the two dropdowns, according to its # type: line.

    The Automation Scripts area at the bottom of the Add Certificate dialog: a short description, then the During issuance script and Post-issuance script dropdowns on the left and right, both showing Select a script

Your first script: from an empty file to a run in the panel

Start with a read-only post-issuance script: it only prints the new certificate's domain and expiry date and changes nothing, so it needs no administrator approval and is the safest one to practise with. After these five steps you have gone through the whole chain (write the script, put it on the server, pick it in the panel, it runs, read the result); every other script works the same way.

  1. Log in over SSH and go to the script folder

    Scripts must be in the shell/ssl/ folder under the Nodekeeper install directory. The default install directory is /opt/nodekeeper; if you changed it at install time, use yours. Log in as root over SSH and run:

    cd /opt/nodekeeper/shell/ssl
    ls
    You should see:

    A list of .sh files such as aws_acm.sh, cloudflare_dns.sh and ssh_deploy.sh: these are the bundled scripts in the panel's dropdowns. Your script goes here too.

  2. Create the script file

    Copy the whole block below into the terminal and press Enter. It creates my_first.sh in the current folder: everything between the line after cat > my_first.sh <<'EOF' and the final EOF is the file content. If you prefer an editor, run nano my_first.sh, paste only the middle part, press Ctrl+O to save and Ctrl+X to quit.

    cat > my_first.sh <<'EOF'
    #!/bin/bash
    # name: 我的第一个脚本 / My first script
    # tip: 只读:打印新证书的域名和到期时间,不改任何东西。/ Read-only: prints the domain and expiry date of the new certificate, changes nothing.
    # type: post
    set -euo pipefail
    
    domain="${1%%[ ,]*}"
    cert_file="${5:-}"
    
    [[ -r "$cert_file" ]] || { echo "找不到证书文件 / certificate file not found: $cert_file" >&2; exit 1; }
    
    echo "域名 / domain: $domain"
    openssl x509 -in "$cert_file" -noout -subject -enddate
    echo "完成 / done"
    EOF
    This lineWhat it does and why
    cat > my_first.sh <<'EOF'Writes everything up to EOF into my_first.sh. The quotes in 'EOF' keep $1 and friends as they are instead of replacing them right away.
    #!/bin/bashThe first line must name the interpreter: it tells the system to run the file with Bash.
    # name: 我的第一个脚本 / My first scriptThe name shown in the dropdown. Write it in both languages so both panel languages read well.
    # tip: ...The description shown under the dropdown when the script is selected. Say what it does and whether it changes anything.
    # type: postPuts it in the Post-issuance script dropdown. With issuing it appears in the During issuance dropdown instead.
    set -euo pipefailSafety switch: stop at the first failing command (with a non-zero exit) and treat unset variables as errors. Almost every script should have it.
    domain="${1%%[ ,]*}"$1 is the certificate's domain. For a multi-domain certificate it is a list; %%[ ,]* cuts everything from the first space or comma, leaving the first domain.
    cert_file="${5:-}"$5 is the path of the certificate file on this server. :- means "empty if not given", so set -u does not complain.
    [[ -r "$cert_file" ]] || { echo "..." >&2; exit 1; }Checks the certificate file exists and is readable; if not, prints the reason and exits with 1. The panel then shows Failed with that reason.
    openssl x509 -in "$cert_file" -noout -subject -enddateReads the certificate with openssl and prints its subject (domain) and expiry date. Read-only.
    echo "完成 / done"Prints a final line. When the script reaches the end normally, the exit code is 0: success.
    You should see:

    Run ls again: my_first.sh is in the list.

  3. Make it executable and try it by hand first

    A new file cannot be executed until you allow it. Then call it the way Nodekeeper does: argument 1 is the domain, 5 the certificate file path, 6 the key file path, with '' (empty) placeholders in between. Find the two paths in the panel: Domains → Certificates, click View on a certificate; the certificate file path and private key file path are in the details (below).

    chmod +x my_first.sh
    ./my_first.sh example.com '' '' '' /path/to/cert.pem /path/to/key.key
    This lineWhat it does and why
    chmod +x my_first.shMakes the file executable. Without it, Nodekeeper reports Permission denied when it runs the script.
    ./my_first.sh example.com '' '' '' ...Replace example.com with your domain and the two paths with the ones from the details. The three '' fill positions 2, 3 and 4 (certificate text, chain, key text, not used here) so the certificate file path lands in position 5.
    Certificate details: status Issued, the expiry date, and the certificate file path and private key file path lines (the paths you need to test a script by hand)
    A real run (certificate ssl-demo.vps.tz)
    $ chmod +x my_first.sh
    $ ls -l my_first.sh
    -rwxr-xr-x 1 root root 517 Nov 28 10:46 my_first.sh
    $ ./my_first.sh ssl-demo.vps.tz '' '' '' '/opt/nodekeeper/certs/ssl-demo.vps.tz,www.ssl-demo.vps.tz/ssl-demo.vps.tz,www.ssl-demo.vps.tz.pem' '/opt/nodekeeper/certs/ssl-demo.vps.tz,www.ssl-demo.vps.tz/ssl-demo.vps.tz,www.ssl-demo.vps.tz.key'
    域名 / domain: ssl-demo.vps.tz
    subject=CN=ssl-demo.vps.tz
    notAfter=Feb 25 23:59:59 2027 GMT
    完成 / done

    If a path in the details does not start with / (for example certs/example.com/example.com.pem), it is relative to the install directory: put /opt/nodekeeper/ in front when you run the script by hand. Put a path with commas (multi-domain certificates) in single quotes.

    You should see:

    The terminal prints the domain line, subject=CN = ..., notAfter=... (the expiry date) and the done line. Run echo $? right after: it shows 0, the exit code of a success.

  4. Pick it in the panel

    Back in the panel, open Domains → Certificates, click Add Certificate and scroll to Automation Scripts at the bottom of the dialog. In the Post-issuance script dropdown on the right, find "我的第一个脚本 / My first script" (the name from your # name: line), select it, fill in the rest of the certificate and save. No restart needed: once the file is in the folder, it is in the dropdown.

    A script can only be chosen when you add a certificate; an existing certificate's script cannot be changed. To practise, import a self-signed certificate as an Existing Certificate (that is what the screenshot does), so you don't wait for a CA.

    Automation Scripts in the Add Certificate dialog: no issuing script, the post-issuance script set to 我的第一个脚本 / My first script, with its tip below (Read-only: prints the domain and expiry date of the new certificate, changes nothing.); Cancel and Save at the bottom right
    You should see:

    The text from your # tip: line appears under the dropdown.

  5. Run it and read the result in the certificate details

    Once the certificate is issued, the script runs by itself, and again after every renewal. If you don't want to wait: click View on the certificate in the list, then Run now in the Post-issue script section (at most once per 30 seconds per certificate), wait a few seconds and click Refresh run record. Last run shows the run time, result, exit code and the end of the script output.

    The Post-issue script section of the certificate details: marked No approval needed; Last run shows result Succeeded, exit code 0 and four output lines: the domain, subject=CN=first-script.nk62.test, notAfter=... and the done line; Run now and Refresh run record at the top right
    You should see:

    Result Succeeded, exit code 0, and the same output you saw in the terminal. The whole chain works.

Something wrong? Check this table

What you seeWhyWhat to do
I can't find where to choose a script on an existing certificateA script is chosen when the certificate is added; it cannot be changed in the details.Add another certificate for the domain with the script selected; to practise, import a self-signed certificate as an Existing Certificate.
My script is not in the dropdownThe file is not in shell/ssl/ or its extension is not .sh; or # type: says issuing, so it is in the During issuance dropdown on the left.Check the name with ls -l /opt/nodekeeper/shell/ssl/; check the # type: post line near the top.
Run failed with Permission deniedThe file is not executable.chmod +x /opt/nodekeeper/shell/ssl/my_first.sh, then Run now.
/bin/bash^M: bad interpreterThe file was edited on Windows and every line ends with an extra carriage return (CRLF).Run sed -i 's/\r$//' my_first.sh; from now on edit on the server with nano / vim, or set your editor's line endings to LF.
openssl: command not foundThe command is not installed on the server.Debian / Ubuntu: apt install openssl; CentOS / Rocky: dnf install openssl. In your own scripts, check every command you use exists on the server.
The manual test prints "certificate file not found"Argument 5 is in the wrong position (one '' missing) or the path is wrong.Count: three '' after the domain, then the certificate file path.
The result stays Failed and it runs again every minuteThe script's exit code is not 0. Nodekeeper retries every minute until it succeeds.Read the last lines of the output in Last run, fix the script, then Run now.

What a post-issuance script receives

A post-issuance script gets positional arguments, the fixed ones first, then your declared parameters in the order declared.

PositionContent
$1The certificate's domain(s), separated by comma or space (use the first for file names)
$2The certificate (PEM text; on a re-run, the site certificate plus the chain)
$3The intermediate chain (PEM, may be empty)
$4The private key (PEM text)
$5Path of the certificate file on this machine
$6Path of the private key file on this machine
$7 onwardsYour declared parameters, in header order
  • Read the files $5 and $6, not the PEM text in $2 and $4. Command-line arguments can be read by other users on the same machine while the script runs; file paths are harmless.
  • With # secrets: env, the argument list is rebuilt from the header: for every param line, in order, the stored value (or its default, or an empty string) goes to that position, so later edits to a script's parameter list never shift the values of older certificates. A password parameter keeps its position but holds an empty string; read the environment variable NK_PARAM_<KEY UPPERCASE> instead, for example ${NK_PARAM_FTP_PASSWORD:-$9} (falling back to the argument when you run it by hand), then unset it.
  • Values never contain line breaks or control characters. Still validate every value before using it in a URL, path or command: a parameter is user input.
  • The three bundled deploy scripts (AWS ACM, Tencent Cloud SSL, SSH deploy) honour NK_SSL_SCRIPT_DRYRUN=1 (check input, print the plan, no network); worth copying so you can try it first. The other bundled scripts do not, and your own script has to implement the check itself (the FTPS example below does), so never expect a dry run from a script you do not know.
Terms explained
PEM
The usual text format for certificates and keys, starting with -----BEGIN CERTIFICATE-----. .pem, .crt and .key files usually hold it.
Certificate chain (intermediates)
The certificates that prove the CA that signed yours is trusted. Browsers need site certificate + chain to validate, so you normally deploy the full chain.
Environment variable
A set of name=value pairs a program gets at start. Only that program and the ones it starts can see them, unlike command-line arguments, which other users can see.
  1. Read the arguments in the script

    Read the arguments you need into named variables at the top of the script, so you don't have to remember what $7 is later. This continues the header from the previous section (three declared parameters):

    domain="${1%%[ ,]*}"      # 第一个域名 / the first domain
    cert_file="${5:-}"        # 证书文件(含证书链)/ certificate file (with chain)
    key_file="${6:-}"         # 私钥文件 / private key file
    host="${7:-}"             # 声明的第 1 个参数 / 1st declared parameter
    user="${8:-}"             # 声明的第 2 个参数 / 2nd declared parameter
    password="${NK_PARAM_PASSWORD:-${9:-}}"; unset NK_PARAM_PASSWORD
    This lineWhat it does and why
    domain="${1%%[ ,]*}"The first domain, handy for naming uploaded files.
    cert_file="${5:-}"The certificate file path (site certificate + chain). Read the file; don't use the PEM text in $2.
    key_file="${6:-}"The private key file path. Read the key on this machine only; never print it or put it on a command line.
    host="${7:-}" / user="${8:-}"Declared parameters 1 and 2, in the order of the header's param lines.
    password="${NK_PARAM_PASSWORD:-${9:-}}"; unset NK_PARAM_PASSWORDPrefer the environment variable (when Nodekeeper runs it), fall back to $9 (your manual test); unset right away so commands started later don't inherit it.

Issuing scripts: automate validation (DNS example)

An issuing script runs while the certificate is waiting for validation: it runs once at the first check of each pending certificate and not again after it succeeds; if it fails, the reason shows in the certificate's renewal status and it is tried again at the next check. For a multi-domain certificate with DNS validation the script is called once per domain, with that domain in $1. Issuing scripts are not approved by anyone, so a script that declares # approval: required cannot be used in this slot.

Arguments: $1 is the domain (may start with *. for a wildcard), $2 is the validation method, then the method-specific items, then the "service config path" (the placeholder 8888888 when the certificate has none: never treat it as a real path), and finally your declared parameters (in header order; with # secrets: env password parameters come through the environment here too).

$2 methodFollowing arguments
dns$3 record type (currently always cname), $4 record host name (full name such as _xxxx.example.com), $5 record value, $6 service config path, your parameters from $7
http / https$3 web root, $4 validation URL, $5 validation file content, $6 service config path, your parameters from $7
email$3 the chosen validation address, $4 service config path, your parameters from $5
email_builtin$3 validation address, $4 MX host @, $5 MX target mx1.host.mw, $6 MX priority 10, $7 service config path, your parameters from $8
  • Check the method first: if the script only supports DNS, fail with a clear message when $2 is not dns.
  • Make it safe to run again: if a record with the same name exists, update it instead of adding a second one (two CNAMEs on one name are usually rejected by the DNS provider).
  • Argument values: Nodekeeper never passes a parameter value that starts with - or contains a newline, but still validate values before putting them into a URL or a command.
  • You don't have to wait for DNS propagation after adding the record: Nodekeeper keeps driving the CA validation on its check cycle.
  1. What an issuing script's form looks like

    Picking an issuing script works like a post-issuance script, only in the dropdown on the left. Below is the bundled Cloudflare DNS validation script: its header declares two parameters, so the form has two fields. Choose DNS validation so the script receives the record to add.

    During issuance script set to Cloudflare DNS验证, with its description and two empty inputs: Cloudflare API Token (required) and Zone ID (leave blank to get it automatically)
  2. A skeleton issuing script: DNS validation

    Below is a skeleton issuing script: it validates its arguments, takes the key from the environment, only works when $2 is dns, updates-or-creates the record, and fails with a bilingual reason. The curl in the middle calls a placeholder address: replace it with the real API of your DNS provider (parameter names and authentication differ per provider). Save it as shell/ssl/my_dns.sh, chmod +x, pick it as the issuing script, and choose DNS validation.

    #!/bin/bash
    # name: My DNS validation / 我的 DNS 验证
    # tip: Adds the certificate validation CNAME at my DNS provider. / 在我的 DNS 服务商那里自动添加证书验证用的 CNAME 记录。
    # type: issuing
    # secrets: env
    # param.api_user: API user / 接口账号|required=true
    # param.api_token: API token / 接口令牌|required=true|password=true
    set -u
    umask 077
    
    domain="${1:-}"; method="${2:-}"
    fail() { echo "错误 / ERROR: $*" >&2; exit 1; }
    [[ "$method" == dns ]] || fail "本脚本只支持 DNS 验证 / this script only supports DNS validation (got: $method)"
    
    rtype="${3:-}"; host="${4:-}"; value="${5:-}"   # $6 = 服务配置路径 / service config path (unused here)
    api_user="${7:-}"
    api_token="${NK_PARAM_API_TOKEN:-${8:-}}"; unset NK_PARAM_API_TOKEN
    
    [[ "$rtype" == cname ]] || fail "不支持的记录类型 / unsupported record type: $rtype"
    [[ "$host"  =~ ^[A-Za-z0-9._-]+$ ]] || fail "记录主机名不合法 / invalid record host: $host"
    [[ "$value" =~ ^[A-Za-z0-9._-]+$ ]] || fail "记录值不合法 / invalid record value"
    [[ -n "$api_user" && -n "$api_token" ]] || fail "请填写 api_user 和 api_token / please fill api_user and api_token"
    
    if [[ "${NK_SSL_SCRIPT_DRYRUN:-}" == 1 ]]; then
      echo "[dry-run] would upsert CNAME $host -> $value for $domain"; exit 0
    fi
    
    # 换成你的 DNS 服务商的接口:同名记录存在就更新,不存在就新建(upsert)。
    # Replace with your DNS provider's API: update the record if the name exists, create it otherwise (upsert).
    # 令牌经 stdin 交给 curl(-K -),不进命令行 / the token goes to curl on stdin (-K -), never on the command line.
    printf 'header = "Authorization: Bearer %s"\n' "$api_token" | curl -sS --fail -K - \
      --connect-timeout 20 --max-time 60 \
      -X PUT "https://dns-api.example.net/v1/records" \
      --data-urlencode "user=$api_user" --data-urlencode "type=CNAME" \
      --data-urlencode "name=$host" --data-urlencode "content=$value" --data-urlencode "ttl=60" >/dev/null \
      || fail "添加验证记录失败 / adding the validation record failed"
    echo "已添加验证记录 / validation record set: $host CNAME $value"

    This is a skeleton, not a working script: dns-api.example.net is a placeholder. Adapt it to your provider's API documentation, run it once by hand with NK_SSL_SCRIPT_DRYRUN=1, then do one real run with a test certificate.

    This lineWhat it does and why
    [[ "$method" == dns ]] || fail "..."This script only handles DNS validation; if the user picked another method, it fails with a clear reason instead of doing half the job.
    rtype="${3:-}"; host="${4:-}"; value="${5:-}"The three DNS items Nodekeeper passes: record type (cname), record host name and record value.
    api_token="${NK_PARAM_API_TOKEN:-${8:-}}"; unset NK_PARAM_API_TOKENThe token comes from the environment ($7 is api_user, $8 the token's placeholder) and is cleared after reading.
    [[ "$host" =~ ^[A-Za-z0-9._-]+$ ]] || fail ...Checks the host name and value only contain safe characters before they go into an API request.
    if [[ "${NK_SSL_SCRIPT_DRYRUN:-}" == 1 ]]; then ... exit 0; fiDry run: print what would happen without touching the network. Use NK_SSL_SCRIPT_DRYRUN=1 when testing by hand.
    printf 'header = ...' | curl ... -K - -X PUT ...Calls the DNS provider to update-or-create the record. The token reaches curl on stdin, not on the command line. Replace this part with your provider's real API.
    echo "已添加验证记录 / validation record set: ..."One line on success, visible in Last run in the panel.

Something wrong? Check this table

What you seeWhyWhat to do
The certificate stays waiting for validation and the details show the script's failureThe issuing script did not exit with 0, for example a wrong token or a wrong API address or parameter.Fix the parameter or the script according to the reason; the next check tries again, no need to recreate the certificate.
Creating the certificate says the script cannot be used as an issuing scriptThe header has # approval: required. The issuing slot has no approval step, so that is not allowed.Keep issuing scripts to validation; move anything that reaches other machines into a post-issuance script.
Only one domain of a multi-domain certificate validatesThe script is called once per domain with different $1, $4 and $5; the script has a domain or host name hard-coded.Always use the values from the arguments; never hard-code the domain.

Make it reliable: exit codes, retries, output, writing files

Exit codes, retries, time limit

  • Exit 0 = success: it is not run again until the next renewal (or "Run now"). Any other code = failure: the status is Failed and Nodekeeper runs the script again every minute until it succeeds, so it must be safe to run repeatedly (idempotent).
  • A script that declares approval and runs longer than 15 minutes is terminated together with everything it started and recorded as Timed out; the output printed before that is kept, so the details show where it hung. Scripts without the approval line have no such limit (unchanged from before), so set timeouts yourself. Don't daemonize. Put a timeout on every network call yourself (curl --connect-timeout 20 --max-time 120, ssh -o ConnectTimeout=20).

Output

  • The certificate details show Last run: time, result, exit code and the last ~4 KiB of output (stdout first, then stderr), visible to the certificate owner and administrators. Print what an operator needs: what was done, where, and why it failed, with the reason in the last lines, in Chinese and English (中文 / English).. Never print secrets: before storing the output Nodekeeper replaces the values of password=true parameters (and of secret-looking names) and private-key PEM blocks with ******, also their base64 and URL-encoded forms and the base64 of user:password (the curl -v Basic header); secrets shorter than 4 characters are not masked. Do not rely on it: other derived values (signed URLs, re-encoded copies) are not recognised
  • Never print secrets: passwords, tokens, keys, the PEM text of $4, set -x output, curl -v headers. Before storing the output Nodekeeper masks the values of password=true parameters (and of parameters with secret-looking names, 4+ characters) and private-key blocks as ******, but don't rely on it: a derived value (a base64 of the password, a signed URL) is not recognised.

Writing safely

Idempotent (running twice gives the same result); atomic writes (write a temporary name in the same directory, then rename, so nobody sees a half-written certificate; keep a .bak when replacing); private keys mode 600 (umask 077), never on a command line or in a log; validate paths (absolute, no .., mind symlinks); quote every variable; no interactive prompts; check required parameters first and fail early and clearly.

Terms explained
Idempotent
Running it once or ten times gives the same result. Failed scripts are re-run every minute, so this is a must: update-or-create, not add another copy each time.
Atomic write
Write to a temporary file in the same folder, then rename it to the real name in one step. Readers see either the old file or the complete new one, never half a file.
Timeout
A maximum wait for every network command (curl --max-time 120), so the script does not hang when the other side doesn't answer.
  1. Write the certificate atomically to another folder

    When a service reads the certificate from a folder, write it like this: temporary name first, keep a backup of the old file, then rename.

    umask 077
    dest=/etc/myapp/tls
    cp "$cert_file" "$dest/.fullchain.pem.new"
    cp "$key_file"  "$dest/.privkey.pem.new"
    [[ -f "$dest/fullchain.pem" ]] && cp -p "$dest/fullchain.pem" "$dest/fullchain.pem.bak"
    mv -f "$dest/.fullchain.pem.new" "$dest/fullchain.pem"
    mv -f "$dest/.privkey.pem.new"  "$dest/privkey.pem"
    This lineWhat it does and why
    umask 077Files created from now on are readable by the owner only (mode 600). Required for the key.
    cp "$cert_file" "$dest/.fullchain.pem.new"Copy to a hidden temporary file first; the real file is untouched.
    [[ -f "$dest/fullchain.pem" ]] && cp -p ... .bakKeep a .bak of the old certificate, so you can switch back by hand if needed.
    mv -f "$dest/.fullchain.pem.new" "$dest/fullchain.pem"A rename within one folder happens in one step: nobody reads a half-written file.

    This writes outside the Nodekeeper folder, so the whole script must declare # approval: required.

Something wrong? Check this table

What you seeWhyWhat to do
The status says Timed outA script that declares approval ran for more than 15 minutes and was stopped together with the processes it started.Check where the output stops; add a timeout to every network command.
The output contains ******Before storing the output, Nodekeeper replaced secret parameter values and private key blocks.That's the protection working, but don't rely on it: never print secrets.
The output starts with "earlier output truncated, only the end is kept"The output was long; only the last 4000 characters or so are kept.Print less progress text and put the failure reason in the last lines.
The same error shows up every minuteA failed script is re-run every minute until it succeeds.Fix the cause, then Run now; keep the script idempotent so re-runs don't repeat actions.

Which scripts must declare # approval: required, and how approval works

  • Declare it when the script reaches another machine or path (SSH, scp/rsync, FTP/FTPS/SFTP, SMB/NFS shares, a local path outside Nodekeeper's own directory) or runs a command, host or path taken from a parameter (a "reload command" or "target host" field). Members can pick any script in shell/ssl and the script runs as root: without approval a parameter would be a remote control. If unsure, declare it; the cost is one click by an administrator.
  • Don't declare it for scripts that only call a cloud API with the key the user typed (AWS ACM, Tencent Cloud SSL, Baidu Cloud CDN, Cloudflare DNS ...): they cannot touch your machines. (The older bundled Nginx helpers nginx_cleanup.sh and reload_nginx.sh predate this rule: they work on this server's own Nginx configuration and do not declare it. Scripts you write should follow the rule above.)
  • Flow: a user creates a certificate, picks the script, fills the parameters and saves → the details show "Awaiting administrator approval", the list shows an "Awaiting approval" badge, and the script does not run → an administrator with the approval permission (by default only the administrator group) clicks "Approve...", checks the script, domains and every parameter in the dialog (secrets shown as ******) and clicks "Approve and run" → the script runs and the result appears in Last run. A row created by an administrator is approved automatically.
  • The whole set is approved: the certificate, its owner, its domains, the script path and every parameter (secrets compared by fingerprint). If any of them changes, the approval is void, the state becomes "Changed since approval - needs approval again" and the script stops running until approved again. An administrator can "Revoke approval" at any time.
  • Limits: the approval covers the script's path and parameters, not the file's contents; whoever can edit files in shell/ssl is already the server's administrator. A certificate whose script only later started to declare approval shows "Awaiting administrator approval" until an administrator approves it once. Old scripts without the line behave exactly as before.
  • Only as a post-issue script: a script that declares approval cannot be chosen as the issue (validation) script. That slot has no approval gate, so the panel refuses it when the certificate is created and Nodekeeper refuses to run it there.
  • Parameters by position: a script without # secrets: env receives its parameters by position, so the stored keys must be exactly the header's param.* keys in the header's order (trailing optional ones may be missing). The panel form does this already; a request that does not is refused, and a stored row that does not match is neither run nor approvable. With # secrets: env the list is rebuilt from the header, so it cannot go wrong.
  • Fail closed: if the script's header cannot be read (file missing or unreadable) the script is not run and the run record says why; a header with non-UTF-8 text (for example GBK comments) is still read.
  1. After saving: awaiting approval

    For a script that declares # approval: required, the details show a yellow Awaiting administrator approval badge after the certificate is saved, and the script does not run.

    The Post-issue script section of the certificate details: a yellow Awaiting administrator approval badge with the note that the script does not run before approval; Approve..., Run now and Refresh run record buttons at the top right
  2. An administrator checks the whole set and approves

    The administrator clicks Approve..., and the dialog lists the script, owner, domains and every parameter (password parameters show as ******). If everything is right, they click Approve and run.

    The approval dialog: a notice that the whole set is being approved, then the script, the owner (redacted), the domains and every parameter, with the key path /root/.ssh/id_ed25519 shown in plain text; Approve and run at the bottom right
  3. Read the result

    After approval the details show Approved, the approver and the time; Last run shows the run time, result, exit code and the end of the script output. This is where you see whether your script reports well.

    The certificate details after approval: status Approved with the approver and time; Last run shows the run time, result Succeeded, exit code 0 and the end of the script output (writing, checksums, reload command, done)

Something wrong? Check this table

What you seeWhyWhat to do
It stays Awaiting administrator approvalNo administrator has approved it yet; members cannot approve.Ask an administrator (by default only the administrator group can approve) to open the details and click Approve...
It changed to Changed since approval - needs approval againThe certificate, owner, domains, script path or a parameter changed, so the old approval no longer applies.An administrator checks the new set and approves again.

Complete examples: post-issuance scripts

Three steps: a minimal script, a complete FTPS upload script, and how to use it in the panel and try it by hand first.

  1. A minimal post-issuance script

    This script copies the new certificate to a fixed folder and then reloads a service. The paths and the service name are written into the script, not taken from panel parameters. It writes to /etc/myapp, outside Nodekeeper's own directory, so it declares # approval: required. Save it as shell/ssl/reload_myapp.sh, run chmod +x on it, then pick "Reload myapp" in the Post-issuance script dropdown; it runs once an administrator has approved it (a certificate created by an administrator is approved automatically). The folder /etc/myapp/tls must already exist, owned by root with mode 0700.

    #!/bin/bash
    # name: Reload myapp
    # tip: After issue or renewal, deploy the certificate to myapp and reload it
    # type: post
    # approval: required
    # param.log_label: Name shown in the log |required=false|default=myapp
    set -euo pipefail
    
    DOMAIN="${1%%[ ,]*}"  # first name on a multi-domain certificate
    CERT_FILE="$5"
    KEY_FILE="$6"
    LABEL="${7:-myapp}"   # your first declared parameter is the 7th argument
    
    [[ -r "$CERT_FILE" && -r "$KEY_FILE" ]] || { echo "[$LABEL] certificate files not found" >&2; exit 1; }
    
    install -m 644 "$CERT_FILE" /etc/myapp/tls/fullchain.pem
    install -m 600 "$KEY_FILE"  /etc/myapp/tls/privkey.pem
    systemctl reload myapp
    echo "[$LABEL] certificate for ${DOMAIN} deployed and reloaded"

    Running it again gives the same result (the same files overwritten, the same service reloaded), and any failing step exits non-zero, so it is retried at the next minute.

    This lineWhat it does and why
    # approval: requiredIt writes to /etc/myapp, outside the Nodekeeper folder, so an administrator must approve it.
    DOMAIN="${1%%[ ,]*}"The first domain, only used in the message.
    LABEL="${7:-myapp}"The first declared parameter is $7; myapp when empty.
    install -m 600 "$KEY_FILE" /etc/myapp/tls/privkey.peminstall copies the file and sets its mode at once: 644 for the certificate, 600 for the key.
    systemctl reload myappMakes the service re-read the certificate. On an error set -e exits non-zero and it is retried a minute later.
  2. A complete example: FTPS upload

    After every issue or renewal, this script uploads <domain>.pem (with the chain) and <domain>.key to your FTP server over FTP with explicit TLS (FTPS). It declares approval (it reaches another machine), keeps the password off the command line (# secrets: env, and curl -K - reads the login from standard input), validates every parameter, uploads under a temporary name and renames afterwards, and ends with a Chinese and English reason on failure. Save it as shell/ssl/ftps_upload.sh and run chmod +x.

    Adapt it to your server and test it once against that server: FTP servers differ in how they handle the rename; this skeleton was checked for syntax and input validation, not against every FTP server.

    #!/bin/bash
    # name: FTPS 上传证书 / Upload certificate over FTPS
    # tip: 签发或续期后,用 FTPS(显式 TLS)把证书和私钥上传到你的 FTP 服务器。会碰到别的机器,需要管理员在面板核准。 / After issue or renewal, uploads the certificate and key to your FTP server over explicit TLS (FTPS). It reaches another machine, so an administrator must approve it in the panel.
    # type: post
    # approval: required
    # secrets: env
    # param.ftp_host: FTP 主机(域名或 IP,可带 :端口)/ FTP host (name or IP, optional :port)|required=true
    # param.ftp_user: FTP 用户名 / FTP user name|required=true
    # param.ftp_password: FTP 密码 / FTP password|required=true|password=true
    # param.remote_dir: 远程目录,相对 FTP 登录目录,例如 ssl / Remote directory, relative to the FTP login directory, e.g. ssl|required=true|default=ssl
    
    set -u
    umask 077
    
    # ---- 1. 读参数:位置参数 1-6 是 Nodekeeper 固定给的,7 起按上面声明的顺序 / Read parameters: 1-6 are fixed by Nodekeeper, 7+ follow the declared order ----
    domain_raw="${1:-}"      # 域名(可能是多个,逗号或空格分隔)/ domain(s), comma or space separated
    cert_file="${5:-}"       # 证书文件路径(完整链)/ certificate file path (full chain)
    key_file="${6:-}"        # 私钥文件路径 / private key file path
    host="${7:-}"
    user="${8:-}"
    pass="${NK_PARAM_FTP_PASSWORD:-${9:-}}"   # 秘密参数走环境变量,$9 只是占位 / the secret comes from the environment, $9 is just a placeholder
    unset NK_PARAM_FTP_PASSWORD
    remote_dir="${10:-}"
    
    fail() { echo "错误 / ERROR: $*" >&2; exit 1; }
    
    # ---- 2. 校验输入(不要信任参数:它们会拼进 URL 和 FTP 命令)/ Validate input (parameters end up in a URL and FTP commands) ----
    first="${domain_raw%%[ ,]*}"
    name="${first//\*/_wildcard_}"
    [[ "$name" =~ ^[A-Za-z0-9._-]+$ ]] || fail "域名不合法 / invalid domain: $first"
    [[ "$host" =~ ^[A-Za-z0-9._:-]+$ ]] || fail "ftp_host 只能含字母数字 . _ : - / ftp_host may only contain letters, digits . _ : -"
    [[ -n "$user" ]] || fail "请填写 ftp_user / please fill ftp_user"
    [[ -n "$pass" ]] || fail "请填写 ftp_password / please fill ftp_password"
    [[ "$remote_dir" =~ ^[A-Za-z0-9._/-]*$ && "$remote_dir" != /* && "$remote_dir" != *..* ]] \
      || fail "remote_dir 必须是相对目录,只含字母数字 . _ / - / remote_dir must be a relative path of letters, digits . _ / -"
    remote_dir="${remote_dir%/}"
    [[ -r "$cert_file" && -r "$key_file" ]] || fail "读不到证书或私钥文件 / cannot read the certificate or key file"
    
    # ---- 3. 演练模式:只说要做什么,不联网 / Dry-run: say what would happen, no network ----
    if [[ "${NK_SSL_SCRIPT_DRYRUN:-}" == 1 ]]; then
      echo "[dry-run] would upload $name.pem and $name.key to ftp://$host/${remote_dir:+$remote_dir/} over explicit TLS"
      exit 0
    fi
    
    # ---- 4. 上传:先传成临时名,传完再改名(对方服务器不会读到半截文件)/ Upload under a temporary name, rename when done (the server never serves a half file) ----
    # 账号密码经 stdin 交给 curl(-K -),不出现在命令行(/proc/<pid>/cmdline 谁都能读)/ The login goes to curl on stdin (-K -), not on the command line (anyone can read /proc/<pid>/cmdline)
    upload() {   # $1 本地文件 local file  $2 最终文件名 final name
      local tmp=".$2.nk-tmp" cred
      cred="$(printf '%s:%s' "$user" "$pass" | sed 's/\\/\\\\/g; s/"/\\"/g')"
      printf 'user = "%s"\n' "$cred" | curl -sS --fail --ssl-reqd --ftp-create-dirs \
        --connect-timeout 20 --max-time 120 -K - \
        -T "$1" -Q "-RNFR $tmp" -Q "-RNTO $2" \
        "ftp://$host/${remote_dir:+$remote_dir/}$tmp"
    }
    
    upload "$cert_file" "$name.pem" || fail "上传证书失败 / uploading the certificate failed"
    upload "$key_file"  "$name.key" || fail "上传私钥失败 / uploading the key failed"
    echo "已上传 / uploaded: $name.pem, $name.key -> $host/${remote_dir:+$remote_dir/}"

    The remote directory is relative to the FTP login directory (for example ssl). If your server uses a self-signed certificate, make the machine running Nodekeeper trust it first; the script has no switch that turns certificate checking off. The private key is uploaded with whatever file mode the FTP server gives new files (often 644): check it once, and keep that directory off any web root and away from other users. The two files are not replaced atomically as a pair: if the key upload fails after the certificate was replaced, the server briefly holds the new certificate with the old key until the next retry (every minute).

    This lineWhat it does and why
    # param.ftp_password: ...|password=trueA password field, stored encrypted; with # secrets: env it is read from NK_PARAM_FTP_PASSWORD.
    remote_dir="${10:-}"The fourth declared parameter, position $10 (two-digit positions need braces).
    [[ "$host" =~ ^[A-Za-z0-9._:-]+$ ]] || fail ...Parameters go into a URL and FTP commands, so each one is checked; anything invalid exits with a reason.
    if [[ "${NK_SSL_SCRIPT_DRYRUN:-}" == 1 ]]; thenDry run: print what would be uploaded, no network.
    printf 'user = "%s"\n' "$cred" | curl ... -K -The login reaches curl on stdin, never on the command line.
    -T "$1" -Q "-RNFR $tmp" -Q "-RNTO $2"Upload under a temporary name, rename when done, so the FTP side never serves half a file.
    upload ... || fail "上传证书失败 / ..."Any failed step exits non-zero with the reason in the last line of the output.
  3. Using the FTPS script: fill in, approve, read the result

    In the certificate form's Post-issuance script, pick "FTPS 上传证书 / Upload certificate over FTPS" (the name in the script's # name: line), and fill in the FTP host, user name, password and remote directory. The password box is a password field, stored encrypted and shown only as ****** after saving. After saving, the details show Awaiting administrator approval; an administrator clicks Approve..., checks the host, user name and remote directory, and clicks Approve and run. Then confirm in Last run of the details: result Succeeded, exit code 0, and the "uploaded" line in the output. If it fails, the reason is in the same place; after you fix the parameters an administrator has to approve once more.

    Try it by hand first: you can run the script yourself the way Nodekeeper does, $1 to $6 first, then the parameters in header order. With # secrets: env the secret is passed twice: an empty placeholder at its position (that is what Nodekeeper puts on the command line) and the real value in NK_PARAM_<KEY UPPERCASE>. For example (the '' after myuser is the password placeholder; drop NK_SSL_SCRIPT_DRYRUN=1 for a real upload; run it as the user Nodekeeper runs as):

    NK_SSL_SCRIPT_DRYRUN=1 NK_PARAM_FTP_PASSWORD=x ./ftps_upload.sh example.com '' '' '' /path/fullchain.pem /path/key.pem ftp.example.com myuser '' ssl

Checklist

  • A #! first line, name, tip, type: post, executable.
  • It reaches another machine or path, or runs something taken from a parameter → declare approval: required.
  • Password parameters use password=true + # secrets: env, read from the environment, never printed, never on a command line.
  • Read $5 and $6, validate every parameter, quote every variable.
  • Idempotent, atomic writes, a timeout on every network call, finishing well under 15 minutes.
  • Exit 0 only on real success; the failure reason is the last output line, in Chinese and English.
  • Tried with NK_SSL_SCRIPT_DRYRUN=1 and once for real; the details show the expected result.

Frequently asked questions

Can I write it in Python or another language?
Yes. Put the interpreter on the first line (for example #!/usr/bin/env python3), keep the .sh extension, and write the header directives as lines starting with #. The interpreter must be installed on the server.
Which user does the script run as?
The user Nodekeeper runs as, normally root. That is why only server administrators can put scripts into shell/ssl; write them as code that runs as root.
If I change the script file, does it need approval again?
No, and it does not trigger one: approval covers the script path and the parameters, not the file content. Whoever can change that folder is already a server administrator; review your changes yourself.
Can I add a script to a certificate that is already issued?
Not by editing it: the script is chosen in Add Certificate. To give an existing domain a script, add another certificate with the script selected.
Does it run on renewal? How often?
Yes. After every issue and every renewal; after a success (exit code 0) it does not run again until the next renewal or a Run now. A failure is retried every minute until it succeeds.
Can members use my script?
Yes. Every account sees the scripts in shell/ssl in the dropdowns. That is why a script that reaches other machines or runs commands from its parameters must declare # approval: required; otherwise a member's parameters would drive a script that runs as root.
How do I try it without touching the network?
Implement the NK_SSL_SCRIPT_DRYRUN=1 check in your script (the FTPS and DNS examples here have it) and set that variable when you run it by hand. Nodekeeper does not set it for you, and a script that doesn't check it runs for real.