For remote monitoring of machines driving an X display (kiosks, dashboards, signage), a periodic screenshot is often the fastest way to see what’s actually on screen. This post shows a TypeScript class that captures the display with scrot, downscales it with ImageMagick’s mogrify, and returns the JPEG in memory — ready to upload to a monitoring server.
Screenshot.ts
import { execFile } from "node:child_process";
import { readFile, unlink } from "node:fs/promises";
import { promisify } from "node:util";
const execFileAsync = promisify(execFile);
export type CheckStatus = "ok" | "warning" | "error";
export interface CheckResult {
status: CheckStatus;
msg: string;
file?: {
value: Buffer;
options: { filename: string; contentType: string };
};
}
export interface ScreenshotCheckerConfig {
/** User running the X session to screenshot */
user?: string;
/** X display to capture */
display?: string;
/** JPEG quality 1-100 */
quality?: number;
/** mogrify -resize argument, e.g. "50%" */
resize?: string;
tempFilename?: string;
}
/**
* Take a low-quality screenshot of an X display using scrot, downscale it with
* ImageMagick's mogrify, and return it as an in-memory file.
*/
export class ScreenshotChecker {
private readonly user: string;
private readonly display: string;
private readonly quality: number;
private readonly resize: string;
private readonly tempFilename: string;
constructor(config: ScreenshotCheckerConfig = {}) {
this.user = config.user ?? "user";
this.display = config.display ?? ":0.0";
this.quality = config.quality ?? 30;
this.resize = config.resize ?? "50%";
this.tempFilename = config.tempFilename ?? "/dev/shm/screenshot.jpg";
}
async check(): Promise<CheckResult> {
try {
await execFileAsync("sudo", [
"-u", this.user,
"env", `DISPLAY=${this.display}`,
"scrot", "-q", String(this.quality), this.tempFilename,
]);
await execFileAsync("mogrify", [
"-resize", this.resize,
"-quality", String(this.quality),
this.tempFilename,
]);
} catch (error: any) {
const code = error?.code;
const stderr: string = error?.stderr ?? "";
if (code === 127 || code === "ENOENT") {
return { status: "error", msg: "Command not found (scrot or mogrify)" };
}
return { status: "error", msg: `Screenshot failed: ${stderr || error?.message || error}` };
}
let data: Buffer;
try {
data = await readFile(this.tempFilename);
} catch (error: any) {
return { status: "error", msg: `Could not read screenshot file: ${error?.message ?? error}` };
} finally {
await unlink(this.tempFilename).catch(() => {});
}
return {
status: "ok",
msg: "Screenshot was successful",
file: {
value: data,
options: { filename: "screenshot.jpg", contentType: "image/jpeg" },
},
};
}
}Install the tools with apt install scrot imagemagick. Since the monitoring process usually runs as a different user (or root) than the X session owner, the capture is executed via sudo -u <user> — grant a passwordless sudoers rule for just that command, e.g.:
/etc/sudoers.d/screenshot
monitoring ALL=(xuser) NOPASSWD: /usr/bin/scrotUsage example
example.ts
import { ScreenshotChecker } from "./Screenshot.ts";
import { writeFile } from "node:fs/promises";
const result = await new ScreenshotChecker({
user: "xuser", // the logged-in X session user
display: ":0.0",
quality: 30, // low quality is enough for monitoring
resize: "50%",
}).check();
if (result.status === "ok" && result.file) {
await writeFile("screenshot.jpg", result.file.value);
// ...or POST result.file to your monitoring server
} else {
console.error(result.msg);
}Note this only works for X11 sessions, not Wayland — on Wayland you’d use grim (wlroots) or a desktop-portal API instead.
Check out similar posts by category:
TypeScript Linux Monitoring
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow