0

I have checked this question regarding crontab failures.

I am running this command to capture the htop command and create a snapshot of it in html format (htop output to human readable file):

/bin/echo q | /usr/bin/htop | /usr/bin/aha --black --line-fix > /tmp/htop.html

This command works correctly on terminal and it doesn't depend on any environment variables. When I add this command as a cronjob, it just generates an empty html file:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- This file was created with the aha Ansi HTML Adapter. http://ziz.delphigl.com/tool_aha.php -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xml+xhtml; charset=UTF-8" />
<title>stdin</title>
</head>
<body style="color:white; background-color:black">
<pre>
</pre>
</body>
</html>

Any reason why it is not able to capture the output via cron?

2

htop needs the TERM variable set to e.g. xterm to output any data, so just set this variable for xterm in your cron line, e.g.:

@hourly echo q|TERM=xterm htop|aha --black --line-fix >/tmp/htop.html

If you want to test whether a command really doesn’t depend on any environment variables – which often lead to problems with cron, see the question you linked –, use env -i to “start with an empty environment”:

$ env -i htop
Error opening terminal: unknown.

Combined with the diff of cron’s and your terminal’s environment that’s a nice way to test which variables a command needs. htop in fact only needs TERM:

env -i TERM=xterm htop
  • +1 for teaching me that env -i htop trick to clean up the environment. – PerlDuck Jun 8 '18 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.